





from __future__ import annotations

import collections
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import is_tf_available, is_vision_available

from ...test_modeling_tf_common import floats_tensor, ids_tensor, random_attention_mask
from ..bert.test_modeling_tf_bert import TFBertModelTester
from ..clip.test_modeling_tf_clip import TFCLIPVisionModelTester
from ..deit.test_modeling_tf_deit import TFDeiTModelTester
from ..roberta.test_modeling_tf_roberta import TFRobertaModelTester
from ..vit.test_modeling_tf_vit import TFViTModelTester


if is_tf_available():
   from transformers import (
       TFBertModel,
       TFCLIPVisionModel,
       TFDeiTModel,
       TFRobertaModel,
       TFVisionTextDualEncoderModel,
       TFViTModel,
       VisionTextDualEncoderConfig,
   )

if is_vision_available():
   from PIL import Image

   from transformers import VisionTextDualEncoderProcessor



def 				lowerCAmelCase_ (							A_):
    if isinstance(A_    ,collections.abc.Iterable):
        return x
    return (x, x)




@require_tf
class        _a    :

      """simple docstring"""


      def 			UpperCAmelCase_       (		self:			Optional[Any]	,    __lowerCamelCase:			Optional[Any]	,    __lowerCamelCase:			Optional[Any]					):
          '''simple docstring'''


          pass






      def 			UpperCAmelCase_       (		self:			List[str]					):
          '''simple docstring'''


          pass






      def 			UpperCAmelCase_       (		self:			str					):
          '''simple docstring'''


          pass






      def 			UpperCAmelCase_       (		self:			List[str]	,    __lowerCamelCase:			Union[str, Any]	,    __lowerCamelCase:			int	,    __lowerCamelCase:			Optional[Any]	,    __lowerCamelCase:			Optional[Any]	,    __lowerCamelCase:			Dict=None	,    **__lowerCamelCase:			Union[str, Any]					):
          '''simple docstring'''


          UpperCamelCase__:	List[Any]					=     VisionTextDualEncoderConfig.from_vision_text_configs(__lowerCamelCase	,    __lowerCamelCase					)

          UpperCamelCase__:	List[Any]					=     TFVisionTextDualEncoderModel(__lowerCamelCase					)

          UpperCamelCase__:	List[Any]					=     model(input_ids=__lowerCamelCase	,    pixel_values=__lowerCamelCase	,    attention_mask=__lowerCamelCase					)

          self.assertEqual(output["text_embeds"].shape	,    (input_ids.shape[0], config.projection_dim)					)
          self.assertEqual(output["image_embeds"].shape	,    (pixel_values.shape[0], config.projection_dim)					)






      def 			UpperCAmelCase_       (		self:			Optional[int]	,    __lowerCamelCase:			Union[str, Any]	,    __lowerCamelCase:			str	,    __lowerCamelCase:			str	,    __lowerCamelCase:			Any	,    __lowerCamelCase:			Union[str, Any]=None	,    **__lowerCamelCase:			Optional[int]					):
          '''simple docstring'''


          UpperCamelCase__				, UpperCamelCase__:	str					=     self.get_vision_text_model(__lowerCamelCase	,    __lowerCamelCase					)
          UpperCamelCase__:	Optional[Any]					=     TFVisionTextDualEncoderModel(vision_model=__lowerCamelCase	,    text_model=__lowerCamelCase					)

          UpperCamelCase__:	List[Any]					=     model(input_ids=__lowerCamelCase	,    pixel_values=__lowerCamelCase	,    attention_mask=__lowerCamelCase					)

          self.assertEqual(output["text_embeds"].shape	,    (input_ids.shape[0], model.config.projection_dim)					)
          self.assertEqual(output["image_embeds"].shape	,    (pixel_values.shape[0], model.config.projection_dim)					)






      def 			UpperCAmelCase_       (		self:			Tuple	,    __lowerCamelCase:			int	,    __lowerCamelCase:			Optional[Any]	,    __lowerCamelCase:			Any	,    __lowerCamelCase:			Optional[Any]	,    __lowerCamelCase:			List[Any]=None	,    **__lowerCamelCase:			str					):
          '''simple docstring'''


          UpperCamelCase__				, UpperCamelCase__:	int					=     self.get_vision_text_model(__lowerCamelCase	,    __lowerCamelCase					)
          UpperCamelCase__:	int					=     {"vision_model": vision_model, "text_model": text_model}
          UpperCamelCase__:	str					=     TFVisionTextDualEncoderModel.from_vision_text_pretrained(**__lowerCamelCase					)

          UpperCamelCase__:	List[Any]					=     model(input_ids=__lowerCamelCase	,    pixel_values=__lowerCamelCase	,    attention_mask=__lowerCamelCase					)

          self.assertEqual(output["text_embeds"].shape	,    (input_ids.shape[0], model.config.projection_dim)					)
          self.assertEqual(output["image_embeds"].shape	,    (pixel_values.shape[0], model.config.projection_dim)					)






      def 			UpperCAmelCase_       (		self:			List[str]	,    __lowerCamelCase:			Any	,    __lowerCamelCase:			str	,    __lowerCamelCase:			List[Any]	,    __lowerCamelCase:			Union[str, Any]	,    __lowerCamelCase:			Any=None	,    **__lowerCamelCase:			Dict					):
          '''simple docstring'''


          UpperCamelCase__				, UpperCamelCase__:	Any					=     self.get_vision_text_model(__lowerCamelCase	,    __lowerCamelCase					)
          UpperCamelCase__:	Optional[Any]					=     TFVisionTextDualEncoderModel(vision_model=__lowerCamelCase	,    text_model=__lowerCamelCase					)

          UpperCamelCase__:	str					=     model(input_ids=__lowerCamelCase	,    pixel_values=__lowerCamelCase	,    attention_mask=__lowerCamelCase					)
          UpperCamelCase__:	List[Any]					=     output[0].numpy()

          with tempfile.TemporaryDirectory() as tmpdirname:
              model.save_pretrained(__lowerCamelCase					)
              UpperCamelCase__:	Dict					=     TFVisionTextDualEncoderModel.from_pretrained(__lowerCamelCase					)

              UpperCamelCase__:	Tuple					=     model(input_ids=__lowerCamelCase	,    pixel_values=__lowerCamelCase	,    attention_mask=__lowerCamelCase					)
              UpperCamelCase__:	Dict					=     after_output[0].numpy()
              UpperCamelCase__:	List[str]					=     np.amax(np.abs(out_a - out_a					)					)
              self.assertLessEqual(__lowerCamelCase	,    1e-5					)






      def 			UpperCAmelCase_       (		self:			Optional[Any]	,    __lowerCamelCase:			Union[str, Any]	,    __lowerCamelCase:			Tuple	,    __lowerCamelCase:			List[str]	,    __lowerCamelCase:			Dict	,    __lowerCamelCase:			Dict=None	,    **__lowerCamelCase:			Union[str, Any]					):
          '''simple docstring'''


          UpperCamelCase__				, UpperCamelCase__:	Any					=     self.get_vision_text_model(__lowerCamelCase	,    __lowerCamelCase					)
          UpperCamelCase__:	List[str]					=     TFVisionTextDualEncoderModel(vision_model=__lowerCamelCase	,    text_model=__lowerCamelCase					)

          UpperCamelCase__:	Dict					=     model(
              input_ids=__lowerCamelCase	,    pixel_values=__lowerCamelCase	,    attention_mask=__lowerCamelCase	,    output_attentions=__lowerCamelCase					)

          UpperCamelCase__:	List[Any]					=     output.vision_model_output.attentions
          self.assertEqual(len(__lowerCamelCase					)	,    vision_config.num_hidden_layers					)

          # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token)
          UpperCamelCase__:	Optional[Any]					=     to_atuple(vision_model.config.image_size					)
          UpperCamelCase__:	Union[str, Any]					=     to_atuple(vision_model.config.patch_size					)
          UpperCamelCase__:	List[Any]					=     (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
          UpperCamelCase__:	List[str]					=     num_patches + 1
          self.assertEqual(vision_attentions[0].shape[-3:]	,    (vision_config.num_attention_heads, seq_len, seq_len)					)

          UpperCamelCase__:	int					=     output.text_model_output.attentions
          self.assertEqual(len(__lowerCamelCase					)	,    text_config.num_hidden_layers					)

          self.assertEqual(
              text_attentions[0].shape[-3:]	,    (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])	,    )






      def 			UpperCAmelCase_       (		self:			Any	,    __lowerCamelCase:			np.ndarray	,    __lowerCamelCase:			np.ndarray	,    __lowerCamelCase:			float					):
          '''simple docstring'''


          UpperCamelCase__:	Any					=     np.abs((a - b)					).max()
          self.assertLessEqual(__lowerCamelCase	,    __lowerCamelCase	,    F"Difference between torch and flax is {diff} (>= {tol})."					)






      def 			UpperCAmelCase_       (		self:			List[Any]					):
          '''simple docstring'''


          UpperCamelCase__:	int					=     self.prepare_config_and_inputs()
          self.check_vision_text_dual_encoder_model(**__lowerCamelCase					)






      def 			UpperCAmelCase_       (		self:			Union[str, Any]					):
          '''simple docstring'''


          UpperCamelCase__:	Optional[Any]					=     self.prepare_config_and_inputs()
          self.check_model_from_pretrained_configs(**__lowerCamelCase					)






      def 			UpperCAmelCase_       (		self:			List[Any]					):
          '''simple docstring'''


          UpperCamelCase__:	Optional[int]					=     self.prepare_config_and_inputs()
          self.check_vision_text_dual_encoder_from_pretrained(**__lowerCamelCase					)






      def 			UpperCAmelCase_       (		self:			str					):
          '''simple docstring'''


          UpperCamelCase__:	Union[str, Any]					=     self.prepare_config_and_inputs()
          self.check_save_load(**__lowerCamelCase					)






      def 			UpperCAmelCase_       (		self:			int					):
          '''simple docstring'''


          UpperCamelCase__:	Optional[Any]					=     self.prepare_config_and_inputs()
          self.check_vision_text_output_attention(**__lowerCamelCase					)






      @slow
      def 			UpperCAmelCase_       (		self:			Any					):
          '''simple docstring'''


          UpperCamelCase__				, UpperCamelCase__:	Optional[int]					=     self.get_pretrained_model_and_inputs()

          UpperCamelCase__:	List[str]					=     model_a(**__lowerCamelCase					)
          UpperCamelCase__:	Union[str, Any]					=     outputs[0].numpy()

          with tempfile.TemporaryDirectory() as tmp_dirname:
              model_a.save_pretrained(__lowerCamelCase					)
              UpperCamelCase__:	Union[str, Any]					=     TFVisionTextDualEncoderModel.from_pretrained(__lowerCamelCase					)

              UpperCamelCase__:	str					=     model_a(**__lowerCamelCase					)
              UpperCamelCase__:	List[str]					=     after_outputs[0].numpy()
              UpperCamelCase__:	str					=     np.amax(np.abs(out_a - out_a					)					)
              self.assertLessEqual(__lowerCamelCase	,    1e-5					)




@require_tf
class        _a    (    UpperCamelCase__						,			unittest.TestCase):

      """simple docstring"""


      def 			UpperCAmelCase_       (		self:			Tuple					):
          '''simple docstring'''


          UpperCamelCase__:	Union[str, Any]					=     TFVisionTextDualEncoderModel.from_vision_text_pretrained(
              "hf-internal-testing/tiny-random-vit"	,    "hf-internal-testing/tiny-random-bert"					)
          UpperCamelCase__:	List[Any]					=     13
          UpperCamelCase__:	Dict					=     floats_tensor(
              [
                  batch_size,
                  model.vision_model.config.num_channels,
                  model.vision_model.config.image_size,
                  model.vision_model.config.image_size,
              ]					)
          UpperCamelCase__:	Optional[int]					=     ids_tensor([batch_size, 4]	,    model.text_model.config.vocab_size					)
          UpperCamelCase__:	Tuple					=     random_attention_mask([batch_size, 4]					)
          UpperCamelCase__:	List[Any]					=     {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

          return model, inputs






      def 			UpperCAmelCase_       (		self:			Optional[int]	,    __lowerCamelCase:			int	,    __lowerCamelCase:			str					):
          '''simple docstring'''


          UpperCamelCase__:	List[str]					=     TFViTModel(__lowerCamelCase	,    name="vision_model"					)
          UpperCamelCase__:	List[Any]					=     TFBertModel(__lowerCamelCase	,    name="text_model"					)
          return vision_model, text_model






      def 			UpperCAmelCase_       (		self:			str					):
          '''simple docstring'''


          UpperCamelCase__:	Optional[Any]					=     TFViTModelTester(self					)
          UpperCamelCase__:	str					=     TFBertModelTester(self					)
          UpperCamelCase__:	Tuple					=     vit_model_tester.prepare_config_and_inputs()
          UpperCamelCase__:	int					=     bert_model_tester.prepare_config_and_inputs()

          UpperCamelCase__				, UpperCamelCase__				, UpperCamelCase__:	List[Any]					=     vision_config_and_inputs

          (
              (
              UpperCamelCase__
          )				, (
              UpperCamelCase__
          )				, (
              UpperCamelCase__
          )				, (
              UpperCamelCase__
          )				, (
              UpperCamelCase__
          )				, (
              UpperCamelCase__
          )				, (
              UpperCamelCase__
          )				, 
          ):	Union[str, Any]					=     text_config_and_inputs

          return {
              "text_config": text_config,
              "vision_config": vision_config,
              "pixel_values": pixel_values,
              "attention_mask": input_mask,
              "input_ids": input_ids,
              "text_token_type_ids": token_type_ids,
              "text_sequence_labels": sequence_labels,
              "text_token_labels": token_labels,
              "text_choice_labels": choice_labels,
          }




@require_tf
class        _a    (    UpperCamelCase__						,			unittest.TestCase):

      """simple docstring"""


      def 			UpperCAmelCase_       (		self:			List[Any]					):
          '''simple docstring'''


          UpperCamelCase__:	List[Any]					=     TFVisionTextDualEncoderModel.from_vision_text_pretrained(
              "Rocketknight1/tiny-random-deit-tf"	,    "hf-internal-testing/tiny-random-roberta"					)
          UpperCamelCase__:	List[Any]					=     13
          UpperCamelCase__:	Union[str, Any]					=     floats_tensor(
              [
                  batch_size,
                  model.vision_model.config.num_channels,
                  model.vision_model.config.image_size,
                  model.vision_model.config.image_size,
              ]					)
          UpperCamelCase__:	List[Any]					=     ids_tensor([batch_size, 4]	,    model.text_model.config.vocab_size					)
          UpperCamelCase__:	List[Any]					=     random_attention_mask([batch_size, 4]					)
          UpperCamelCase__:	Union[str, Any]					=     {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

          return model, inputs






      def 			UpperCAmelCase_       (		self:			int	,    __lowerCamelCase:			Optional[Any]	,    __lowerCamelCase:			Optional[int]	,    __lowerCamelCase:			Union[str, Any]	,    __lowerCamelCase:			int	,    __lowerCamelCase:			Optional[Any]=None	,    **__lowerCamelCase:			Tuple					):
          '''simple docstring'''


          UpperCamelCase__				, UpperCamelCase__:	int					=     self.get_vision_text_model(__lowerCamelCase	,    __lowerCamelCase					)
          UpperCamelCase__:	str					=     TFVisionTextDualEncoderModel(vision_model=__lowerCamelCase	,    text_model=__lowerCamelCase					)

          UpperCamelCase__:	Tuple					=     model(
              input_ids=__lowerCamelCase	,    pixel_values=__lowerCamelCase	,    attention_mask=__lowerCamelCase	,    output_attentions=__lowerCamelCase					)

          UpperCamelCase__:	Any					=     output.vision_model_output.attentions
          self.assertEqual(len(__lowerCamelCase					)	,    vision_config.num_hidden_layers					)

          # in DEiT, the seq_len equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens)
          UpperCamelCase__:	int					=     to_atuple(vision_model.config.image_size					)
          UpperCamelCase__:	List[Any]					=     to_atuple(vision_model.config.patch_size					)
          UpperCamelCase__:	Any					=     (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
          UpperCamelCase__:	List[str]					=     num_patches + 2
          self.assertEqual(vision_attentions[0].shape[-3:]	,    (vision_config.num_attention_heads, seq_len, seq_len)					)

          UpperCamelCase__:	int					=     output.text_model_output.attentions
          self.assertEqual(len(__lowerCamelCase					)	,    text_config.num_hidden_layers					)

          self.assertEqual(
              text_attentions[0].shape[-3:]	,    (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])	,    )






      def 			UpperCAmelCase_       (		self:			str	,    __lowerCamelCase:			Optional[int]	,    __lowerCamelCase:			Dict					):
          '''simple docstring'''


          UpperCamelCase__:	Any					=     TFDeiTModel(__lowerCamelCase	,    name="vision_model"					)
          UpperCamelCase__:	Dict					=     TFRobertaModel(__lowerCamelCase	,    name="text_model"					)
          return vision_model, text_model






      def 			UpperCAmelCase_       (		self:			Tuple					):
          '''simple docstring'''


          UpperCamelCase__:	Union[str, Any]					=     TFDeiTModelTester(self					)
          UpperCamelCase__:	Optional[int]					=     TFRobertaModelTester(self					)
          UpperCamelCase__:	List[Any]					=     vit_model_tester.prepare_config_and_inputs()
          UpperCamelCase__:	Tuple					=     bert_model_tester.prepare_config_and_inputs()

          UpperCamelCase__				, UpperCamelCase__				, UpperCamelCase__:	Optional[Any]					=     vision_config_and_inputs

          (
              (
              UpperCamelCase__
          )				, (
              UpperCamelCase__
          )				, (
              UpperCamelCase__
          )				, (
              UpperCamelCase__
          )				, (
              UpperCamelCase__
          )				, (
              UpperCamelCase__
          )				, (
              UpperCamelCase__
          )				, 
          ):	Any					=     text_config_and_inputs

          return {
              "text_config": text_config,
              "vision_config": vision_config,
              "pixel_values": pixel_values,
              "attention_mask": input_mask,
              "input_ids": input_ids,
              "text_token_type_ids": token_type_ids,
              "text_sequence_labels": sequence_labels,
              "text_token_labels": token_labels,
              "text_choice_labels": choice_labels,
          }




@require_tf
class        _a    (    UpperCamelCase__						,			unittest.TestCase):

      """simple docstring"""


      def 			UpperCAmelCase_       (		self:			Optional[Any]					):
          '''simple docstring'''


          UpperCamelCase__:	Union[str, Any]					=     TFVisionTextDualEncoderModel.from_vision_text_pretrained(
              "Rocketknight1/tiny-random-clip-tf"	,    "hf-internal-testing/tiny-random-bert"					)
          UpperCamelCase__:	Any					=     13
          UpperCamelCase__:	int					=     floats_tensor(
              [
                  batch_size,
                  model.vision_model.config.num_channels,
                  model.vision_model.config.image_size,
                  model.vision_model.config.image_size,
              ]					)
          UpperCamelCase__:	Optional[int]					=     ids_tensor([batch_size, 4]	,    model.text_model.config.vocab_size					)
          UpperCamelCase__:	Any					=     random_attention_mask([batch_size, 4]					)
          UpperCamelCase__:	str					=     {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

          return model, inputs






      def 			UpperCAmelCase_       (		self:			Dict	,    __lowerCamelCase:			Optional[Any]	,    __lowerCamelCase:			Tuple					):
          '''simple docstring'''


          UpperCamelCase__:	List[Any]					=     TFCLIPVisionModel(__lowerCamelCase	,    name="vision_model"					)
          UpperCamelCase__:	Any					=     TFBertModel(__lowerCamelCase	,    name="text_model"					)
          return vision_model, text_model






      def 			UpperCAmelCase_       (		self:			Dict					):
          '''simple docstring'''


          UpperCamelCase__:	Any					=     TFCLIPVisionModelTester(self					)
          UpperCamelCase__:	Union[str, Any]					=     TFBertModelTester(self					)
          UpperCamelCase__:	List[str]					=     clip_model_tester.prepare_config_and_inputs()
          UpperCamelCase__:	str					=     bert_model_tester.prepare_config_and_inputs()

          UpperCamelCase__				, UpperCamelCase__:	List[Any]					=     vision_config_and_inputs

          (
              (
              UpperCamelCase__
          )				, (
              UpperCamelCase__
          )				, (
              UpperCamelCase__
          )				, (
              UpperCamelCase__
          )				, (
              UpperCamelCase__
          )				, (
              UpperCamelCase__
          )				, (
              UpperCamelCase__
          )				, 
          ):	Tuple					=     text_config_and_inputs

          return {
              "text_config": text_config,
              "vision_config": vision_config,
              "pixel_values": pixel_values,
              "attention_mask": input_mask,
              "input_ids": input_ids,
              "text_token_type_ids": token_type_ids,
              "text_sequence_labels": sequence_labels,
              "text_token_labels": token_labels,
              "text_choice_labels": choice_labels,
          }




@require_vision
@require_tf
class        _a    (    unittest.TestCase):

      """simple docstring"""


      @slow
      def 			UpperCAmelCase_       (		self:			List[str]					):
          '''simple docstring'''


          UpperCamelCase__:	Any					=     TFVisionTextDualEncoderModel.from_pretrained(
              "clip-italian/clip-italian"	,    logit_scale_init_value=1.0	,    from_pt=__lowerCamelCase					)
          UpperCamelCase__:	Optional[Any]					=     VisionTextDualEncoderProcessor.from_pretrained("clip-italian/clip-italian"					)

          UpperCamelCase__:	Union[str, Any]					=     Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"					)
          UpperCamelCase__:	Any					=     processor(
              text=["una foto di un gatto", "una foto di un cane"]	,    images=__lowerCamelCase	,    padding=__lowerCamelCase	,    return_tensors="np"					)

          UpperCamelCase__:	Optional[int]					=     model(**__lowerCamelCase					)

          # verify the logits
          self.assertEqual(outputs.logits_per_image.shape	,    (inputs.pixel_values.shape[0], inputs.input_ids.shape[0])					)
          self.assertEqual(
              outputs.logits_per_text.shape	,    (inputs.input_ids.shape[0], inputs.pixel_values.shape[0])	,    )

          UpperCamelCase__:	List[str]					=     np.array([[1.2_284_727, 0.3_104_122]]					)

          self.assertTrue(np.allclose(outputs.logits_per_image.numpy()	,    __lowerCamelCase	,    atol=1e-3					)					)








from __future__ import annotations

from typing import Any




class        _a    :

      """simple docstring"""


      def __init__(		self:			Optional[int]	,    __lowerCamelCase:			int					):
          '''simple docstring'''


          UpperCamelCase__:	Optional[Any]					=     num_of_nodes
          UpperCamelCase__:	list[list[int]]					=     []
          UpperCamelCase__:	dict[int, int]					=     {}






      def 			UpperCAmelCase_       (		self:			Any	,    __lowerCamelCase:			int	,    __lowerCamelCase:			int	,    __lowerCamelCase:			int					):
          '''simple docstring'''


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






      def 			UpperCAmelCase_       (		self:			Dict	,    __lowerCamelCase:			int					):
          '''simple docstring'''


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






      def 			UpperCAmelCase_       (		self:			str	,    __lowerCamelCase:			int					):
          '''simple docstring'''


          if self.m_component[u_node] != u_node:
              for k in self.m_component:
                  UpperCamelCase__:	Dict					=     self.find_component(__lowerCamelCase					)






      def 			UpperCAmelCase_       (		self:			str	,    __lowerCamelCase:			list[int]	,    __lowerCamelCase:			int	,    __lowerCamelCase:			int					):
          '''simple docstring'''


          if component_size[u_node] <= component_size[v_node]:
              UpperCamelCase__:	List[str]					=     v_node
              component_size[v_node] += component_size[u_node]
              self.set_component(__lowerCamelCase					)

          elif component_size[u_node] >= component_size[v_node]:
              UpperCamelCase__:	Any					=     self.find_component(__lowerCamelCase					)
              component_size[u_node] += component_size[v_node]
              self.set_component(__lowerCamelCase					)






      def 			UpperCAmelCase_       (		self:			Tuple					):
          '''simple docstring'''


          UpperCamelCase__:	Optional[int]					=     []
          UpperCamelCase__:	Optional[int]					=     0

          UpperCamelCase__:	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					)

          UpperCamelCase__:	Optional[int]					=     self.m_num_of_nodes

          while num_of_components > 1:
              for edge in self.m_edges:
                  UpperCamelCase__				, UpperCamelCase__				, UpperCamelCase__:	List[str]					=     edge

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

              for edge in minimum_weight_edge:
                  if isinstance(__lowerCamelCase	,    __lowerCamelCase					):
                      UpperCamelCase__				, UpperCamelCase__				, UpperCamelCase__:	List[str]					=     edge

                      UpperCamelCase__:	Tuple					=     self.m_component[u]
                      UpperCamelCase__:	Optional[Any]					=     self.m_component[v]

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

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



def 				lowerCAmelCase_ (							):
    pass


if __name__ == "__main__":
   import doctest

   doctest.testmod()

