

class WeightedQuickUnion(object):

    def __init__(self):
        self.id = []
        self.weight = []

    def find(self, val):
        p = val
        while self.id[p] != p:
            p = self.id[p]

        return self.id[p]

    def union(self, p, q):
        p_root = self.find(p)
        q_root = self.find(q)

        if p_root == q_root:
            return

        self.id[q_root] = p_root

    def is_connected(self, p, q):
        return self.find(p) == self.find(q)



class WeightedQuickUnion(object):

    def __init__(self, data=None):
        self.id = data
        self.weight = [1] * len(data)
        self.count = len(data)

    def count(self):
        return self.count

    def find(self, val):
        p = val
        while self.id[p] != p:
            p = self.id[p]

        return p

    def union(self, p, q):
        p_root = self.find(p)
        q_root = self.find(q)

        if p_root == q_root:
            return

        self.id[q_root] = p_root
        self.count -= 1

    def is_connected(self, p, q):
        return self.find(p) == self.find(q)

Fix quick union functions issue
Fix quick union functions issue

Missing counter
Find function should return position of element
Decrement counter in union

  
  
  class WeightedQuickUnion(object):
  
-     def __init__(self):
+     def __init__(self, data=None):
-         self.id = []
+         self.id = data
-         self.weight = []
+         self.weight = [1] * len(data)
+         self.count = len(data)
+ 
+     def count(self):
+         return self.count
  
      def find(self, val):
          p = val
          while self.id[p] != p:
              p = self.id[p]
  
-         return self.id[p]
+         return p
  
      def union(self, p, q):
          p_root = self.find(p)
          q_root = self.find(q)
  
          if p_root == q_root:
              return
  
          self.id[q_root] = p_root
+         self.count -= 1
  
      def is_connected(self, p, q):
          return self.find(p) == self.find(q)
  
Fix quick union functions issue
## Code Before:


class WeightedQuickUnion(object):

    def __init__(self):
        self.id = []
        self.weight = []

    def find(self, val):
        p = val
        while self.id[p] != p:
            p = self.id[p]

        return self.id[p]

    def union(self, p, q):
        p_root = self.find(p)
        q_root = self.find(q)

        if p_root == q_root:
            return

        self.id[q_root] = p_root

    def is_connected(self, p, q):
        return self.find(p) == self.find(q)

## Instruction:
Fix quick union functions issue
## Code After:


class WeightedQuickUnion(object):

    def __init__(self, data=None):
        self.id = data
        self.weight = [1] * len(data)
        self.count = len(data)

    def count(self):
        return self.count

    def find(self, val):
        p = val
        while self.id[p] != p:
            p = self.id[p]

        return p

    def union(self, p, q):
        p_root = self.find(p)
        q_root = self.find(q)

        if p_root == q_root:
            return

        self.id[q_root] = p_root
        self.count -= 1

    def is_connected(self, p, q):
        return self.find(p) == self.find(q)
