


"""simple docstring"""







from __future__ import annotations

from typing import Any


class 				SCREAMING_SNAKE_CASE__    :
       """simple docstring"""


       def __init__(						self		,					snake_case__						):






         """simple docstring"""





         lowerCAmelCase					:					int								=							num_of_nodes
         lowerCAmelCase					:					list[list[int]]								=							[]
         lowerCAmelCase					:					dict[int, int]								=							{}

       def 	lowercase__							(						self		,					snake_case__		,					snake_case__		,					snake_case__						):






         """simple docstring"""





         self.m_edges.append([u_node, v_node, weight]						)

       def 	lowercase__							(						self		,					snake_case__						):






         """simple docstring"""





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

       def 	lowercase__							(						self		,					snake_case__						):






         """simple docstring"""





         if self.m_component[u_node] != u_node:
           for k in self.m_component:
             lowerCAmelCase					:					Optional[int]								=							self.find_component(snake_case__						)

       def 	lowercase__							(						self		,					snake_case__		,					snake_case__		,					snake_case__						):






         """simple docstring"""





         if component_size[u_node] <= component_size[v_node]:
           lowerCAmelCase					:					Any								=							v_node
           component_size[v_node] += component_size[u_node]
           self.set_component(snake_case__						)

         elif component_size[u_node] >= component_size[v_node]:
           lowerCAmelCase					:					str								=							self.find_component(snake_case__						)
           component_size[u_node] += component_size[v_node]
           self.set_component(snake_case__						)







       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Tuple								=							[]
         lowerCAmelCase					:					Optional[int]								=							0

         lowerCAmelCase					:					list[Any]								=							[-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						)

         lowerCAmelCase					:					List[Any]								=							self.m_num_of_nodes

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

             lowerCAmelCase					:					Optional[int]								=							self.m_component[u]
             lowerCAmelCase					:					List[Any]								=							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
                 ):
                   lowerCAmelCase					:					int								=							[u, v, w]

           for edge in minimum_weight_edge:
             if isinstance(snake_case__		,					snake_case__						):
               lowerCAmelCase						,  lowerCAmelCase						,  lowerCAmelCase					:					Optional[int]								=							edge

               lowerCAmelCase					:					List[str]								=							self.m_component[u]
               lowerCAmelCase					:					List[Any]								=							self.m_component[v]

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

           lowerCAmelCase					:					Union[str, Any]								=							[-1] * self.m_num_of_nodes
         print(f"""The total weight of the minimal spanning tree is: {mst_weight}"""						)

def  a__      (    ):


  '''simple docstring'''









if __name__ == "__main__":
     import doctest

     doctest.testmod()









"""simple docstring"""







import pytest

from datasets.parallel import ParallelBackendConfig, parallel_backend
from datasets.utils.py_utils import map_nested

from .utils import require_dill_gt_0_3_2, require_joblibspark, require_not_windows
def  a__      (    SCREAMING_SNAKE_CASE   :   str    ):  # picklable for multiprocessing


  '''simple docstring'''







  return i + 1
@require_dill_gt_0_3_2
@require_joblibspark
@require_not_windows
def  a__      (    ):


  '''simple docstring'''







  with parallel_backend("spark"    ):
    assert ParallelBackendConfig.backend_name == "spark"

  lowerCAmelCase					:					List[str]								=							[1, 2, 3]
  with pytest.raises(SCREAMING_SNAKE_CASE    ):
    with parallel_backend("unsupported backend"    ):
      map_nested(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    ,				num_proc=2    )

  with pytest.raises(SCREAMING_SNAKE_CASE    ):
    with parallel_backend("unsupported backend"    ):
      map_nested(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    ,				num_proc=-1    )





@require_dill_gt_0_3_2
@require_joblibspark
@require_not_windows
@pytest.mark.parametrize("num_proc"    ,				[2, -1]    )
def  a__      (    SCREAMING_SNAKE_CASE   :   Tuple    ):


  '''simple docstring'''







  lowerCAmelCase					:					Tuple								=							[1, 2]
  lowerCAmelCase					:					int								=							{"a": 1, "b": 2}
  lowerCAmelCase					:					List[str]								=							{"a": [1, 2], "b": [3, 4]}
  lowerCAmelCase					:					Dict								=							{"a": {"1": 1}, "b": 2}
  lowerCAmelCase					:					Tuple								=							{"a": 1, "b": 2, "c": 3, "d": 4}
  lowerCAmelCase					:					Any								=							[2, 3]
  lowerCAmelCase					:					Any								=							{"a": 2, "b": 3}
  lowerCAmelCase					:					Optional[int]								=							{"a": [2, 3], "b": [4, 5]}
  lowerCAmelCase					:					Optional[int]								=							{"a": {"1": 2}, "b": 3}
  lowerCAmelCase					:					str								=							{"a": 2, "b": 3, "c": 4, "d": 5}

  with parallel_backend("spark"    ):
    assert map_nested(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    ,				num_proc=SCREAMING_SNAKE_CASE    ) == expected_map_nested_sa
    assert map_nested(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    ,				num_proc=SCREAMING_SNAKE_CASE    ) == expected_map_nested_sa
    assert map_nested(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    ,				num_proc=SCREAMING_SNAKE_CASE    ) == expected_map_nested_sa
    assert map_nested(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    ,				num_proc=SCREAMING_SNAKE_CASE    ) == expected_map_nested_sa
    assert map_nested(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    ,				num_proc=SCREAMING_SNAKE_CASE    ) == expected_map_nested_sa





