

"""simple docstring"""





class  _lowerCAmelCase  :







 """simple docstring"""







 def __init__(      self							:      Tuple     ):
    __lowercase								=						""
    __lowercase								=						""
    __lowercase								=						[]


 def  _lowercase			(      self							:      Dict, UpperCAmelCase__							:      int, UpperCAmelCase__							:      int     ):
    if m == -1:
       return n + 1
    elif n == -1:
       return m + 1
    elif self.dp[m][n] > -1:
       return self.dp[m][n]
    else:
       if self.worda[m] == self.worda[n]:
          __lowercase								=						self.__min_dist_top_down_dp(m - 1, n - 1     )
       else:
          __lowercase								=						self.__min_dist_top_down_dp(UpperCAmelCase__, n - 1     )
          __lowercase								=						self.__min_dist_top_down_dp(m - 1, UpperCAmelCase__     )
          __lowercase								=						self.__min_dist_top_down_dp(m - 1, n - 1     )
          __lowercase								=						1 + min(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__     )

       return self.dp[m][n]


 def  _lowercase			(      self							:      List[str], UpperCAmelCase__							:      str, UpperCAmelCase__							:      str     ):
    __lowercase								=						worda
    __lowercase								=						worda
    __lowercase								=						[[-1 for _ in range(len(UpperCAmelCase__     )     )] for _ in range(len(UpperCAmelCase__     )     )]

    return self.__min_dist_top_down_dp(len(UpperCAmelCase__     ) - 1, len(UpperCAmelCase__     ) - 1     )



 def  _lowercase			(      self							:      str, UpperCAmelCase__							:      str, UpperCAmelCase__							:      str     ):
    __lowercase								=						worda
    __lowercase								=						worda
    __lowercase								=						len(UpperCAmelCase__     )
    __lowercase								=						len(UpperCAmelCase__     )
    __lowercase								=						[[0 for _ in range(n + 1     )] for _ in range(m + 1     )]

    for i in range(m + 1     ):
       for j in range(n + 1     ):
          if i == 0:  # first string is empty
             __lowercase								=						j
          elif j == 0:  # second string is empty
             __lowercase								=						i
          elif worda[i - 1] == worda[j - 1]:  # last characters are equal
             __lowercase								=						self.dp[i - 1][j - 1]
          else:
             __lowercase								=						self.dp[i][j - 1]
             __lowercase								=						self.dp[i - 1][j]
             __lowercase								=						self.dp[i - 1][j - 1]
             __lowercase								=						1 + min(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__     )
    return self.dp[m][n]


if __name__ == "__main__":
   _a :    Union[str, Any]							=							EditDistance()

   print('****************** Testing Edit Distance DP Algorithm ******************')
   print()

   _a :    List[str]							=							input('Enter the first string: ').strip()
   _a :    Union[str, Any]							=							input('Enter the second string: ').strip()

   print()
   print(F"The minimum edit distance is: {solver.min_dist_top_down(Sa, Sa)}")
   print(F"The minimum edit distance is: {solver.min_dist_bottom_up(Sa, Sa)}")
   print()
   print('*************** End of Testing Edit Distance DP Algorithm ***************')







"""simple docstring"""





from __future__ import annotations

from typing import Any




class  _lowerCAmelCase  :







 """simple docstring"""







 def __init__(      self							:      Union[str, Any], UpperCAmelCase__							:      int     ):
    __lowercase								=						num_of_nodes
    __lowercase								=						[]
    __lowercase								=						{}


 def  _lowercase			(      self							:      Tuple, UpperCAmelCase__							:      int, UpperCAmelCase__							:      int, UpperCAmelCase__							:      int     ):
    self.m_edges.append([u_node, v_node, weight]     )


 def  _lowercase			(      self							:      Dict, UpperCAmelCase__							:      int     ):

    if self.m_component[u_node] == u_node:
       return u_node
    return self.find_component(self.m_component[u_node]     )


 def  _lowercase			(      self							:      Dict, UpperCAmelCase__							:      int     ):

    if self.m_component[u_node] != u_node:
       for k in self.m_component:
          __lowercase								=						self.find_component(UpperCAmelCase__     )


 def  _lowercase			(      self							:      str, UpperCAmelCase__							:      list[int], UpperCAmelCase__							:      int, UpperCAmelCase__							:      int     ):

    if component_size[u_node] <= component_size[v_node]:
       __lowercase								=						v_node
       component_size[v_node] += component_size[u_node]
       self.set_component(UpperCAmelCase__     )

    elif component_size[u_node] >= component_size[v_node]:
       __lowercase								=						self.find_component(UpperCAmelCase__     )
       component_size[u_node] += component_size[v_node]
       self.set_component(UpperCAmelCase__     )



 def  _lowercase			(      self							:      Tuple     ):
    __lowercase								=						[]
    __lowercase								=						0

    __lowercase								=						[-1] * self.m_num_of_nodes

    # A list of components (initialized to all of the nodes)
    for node in range(self.m_num_of_nodes     ):
       self.m_component.update({node: node}     )
       component_size.append(1     )

    __lowercase								=						self.m_num_of_nodes

    while num_of_components > 1:
       for edge in self.m_edges:
          __lowercase		,__lowercase		,__lowercase								=						edge

          __lowercase								=						self.m_component[u]
          __lowercase								=						self.m_component[v]

          if u_component != v_component:

             for component in (u_component, v_component):
                if (
                    minimum_weight_edge[component] == -1
                    or minimum_weight_edge[component][2] > w
                ):
                   __lowercase								=						[u, v, w]

       for edge in minimum_weight_edge:
          if isinstance(UpperCAmelCase__, UpperCAmelCase__     ):
             __lowercase		,__lowercase		,__lowercase								=						edge

             __lowercase								=						self.m_component[u]
             __lowercase								=						self.m_component[v]

             if u_component != v_component:
                mst_weight += w
                self.union(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__     )
                print(F"""Added edge [{u} - {v}]\nAdded weight: {w}\n"""     )
                num_of_components -= 1

       __lowercase								=						[-1] * self.m_num_of_nodes
    print(F"""The total weight of the minimal spanning tree is: {mst_weight}"""     )







def   _A  (  )     ->						None:
   '''simple docstring'''




if __name__ == "__main__":
   import doctest

   doctest.testmod()




