

from collections import deque
from math import floor
from random import random
from time import time



class   lowercase_   :



      def __init__(      self				:    Any		)   ->					Union[str, Any]:
        _snake_case						=							{}



      def 						UpperCamelCase_		(      self				:    Dict      , A__				:    List[str]      , A__				:    Dict      , A__				:    Dict=1		)   ->					List[str]:
        if self.graph.get(A__		):
          if self.graph[u].count([w, v]		) == 0:
            self.graph[u].append([w, v]		)
        else:
          _snake_case						=							[[w, v]]
        if not self.graph.get(A__		):
          _snake_case						=							[]



      def 						UpperCamelCase_		(      self				:    Tuple		)   ->					str:
        return list(self.graph		)



      def 						UpperCamelCase_		(      self				:    Union[str, Any]      , A__				:    List[Any]      , A__				:    Any		)   ->					Union[str, Any]:
        if self.graph.get(A__		):
          for _ in self.graph[u]:
            if _[1] == v:
              self.graph[u].remove(A__		)



      def 						UpperCamelCase_		(      self				:    str      , A__				:    Optional[int]=-2      , A__				:    List[str]=-1		)   ->					Tuple:
        if s == d:
          return []
        _snake_case						=							[]
        _snake_case						=							[]
        if s == -2:
          _snake_case						=							list(self.graph		)[0]
        stack.append(A__		)
        visited.append(A__		)
        _snake_case						=							s

        while True:
          # check if there is any non isolated nodes
          if len(self.graph[s]		) != 0:
            _snake_case						=							s
            for node in self.graph[s]:
              if visited.count(node[1]		) < 1:
                if node[1] == d:
                  visited.append(A__		)
                  return visited
                else:
                  stack.append(node[1]		)
                  visited.append(node[1]		)
                  _snake_case						=							node[1]
                  break

            # check if all the children are visited
          if s == ss:
            stack.pop()
            if len(A__		) != 0:
              _snake_case						=							stack[len(A__		) - 1]
          else:
            _snake_case						=							ss

          # check if se have reached the starting point
          if len(A__		) == 0:
            return visited



      def 						UpperCamelCase_		(      self				:    Union[str, Any]      , A__				:    str=-1		)   ->					List[str]:
        if c == -1:
          _snake_case						=							floor(random() * 10000		) + 10
        for i in range(A__		):
          # every vertex has max 100 edges
          for _ in range(floor(random() * 102		) + 1		):
            _snake_case						=							floor(random() * c		) + 1
            if n != i:
              self.add_pair(A__      , A__      , 1		)



      def 						UpperCamelCase_		(      self				:    Union[str, Any]      , A__				:    List[str]=-2		)   ->					Union[str, Any]:
        _snake_case						=							deque()
        _snake_case						=							[]
        if s == -2:
          _snake_case						=							list(self.graph		)[0]
        d.append(A__		)
        visited.append(A__		)
        while d:
          _snake_case						=							d.popleft()
          if len(self.graph[s]		) != 0:
            for node in self.graph[s]:
              if visited.count(node[1]		) < 1:
                d.append(node[1]		)
                visited.append(node[1]		)
        return visited



      def 						UpperCamelCase_		(      self				:    Union[str, Any]      , A__				:    Any		)   ->					Tuple:
        _snake_case						=							0
        for x in self.graph:
          for y in self.graph[x]:
            if y[1] == u:
              count += 1
        return count



      def 						UpperCamelCase_		(      self				:    List[Any]      , A__				:    Any		)   ->					Dict:
        return len(self.graph[u]		)



      def 						UpperCamelCase_		(      self				:    Optional[Any]      , A__				:    int=-2		)   ->					Optional[Any]:
        _snake_case						=							[]
        _snake_case						=							[]
        if s == -2:
          _snake_case						=							list(self.graph		)[0]
        stack.append(A__		)
        visited.append(A__		)
        _snake_case						=							s
        _snake_case						=							[]

        while True:
          # check if there is any non isolated nodes
          if len(self.graph[s]		) != 0:
            _snake_case						=							s
            for node in self.graph[s]:
              if visited.count(node[1]		) < 1:
                stack.append(node[1]		)
                visited.append(node[1]		)
                _snake_case						=							node[1]
                break

            # check if all the children are visited
          if s == ss:
            sorted_nodes.append(stack.pop()		)
            if len(A__		) != 0:
              _snake_case						=							stack[len(A__		) - 1]
          else:
            _snake_case						=							ss

          # check if se have reached the starting point
          if len(A__		) == 0:
            return sorted_nodes



      def 						UpperCamelCase_		(      self				:    Tuple		)   ->					List[str]:
        _snake_case						=							[]
        _snake_case						=							[]
        _snake_case						=							list(self.graph		)[0]
        stack.append(A__		)
        visited.append(A__		)
        _snake_case						=							-2
        _snake_case						=							[]
        _snake_case						=							s
        _snake_case						=							False
        _snake_case						=							set()

        while True:
          # check if there is any non isolated nodes
          if len(self.graph[s]		) != 0:
            _snake_case						=							s
            for node in self.graph[s]:
              if (
                  visited.count(node[1]		) > 0
                  and node[1] != parent
                  and indirect_parents.count(node[1]		) > 0
                  and not on_the_way_back
              ):
                _snake_case						=							len(A__		) - 1
                while len_stack >= 0:
                  if stack[len_stack] == node[1]:
                    anticipating_nodes.add(node[1]		)
                    break
                  else:
                    anticipating_nodes.add(stack[len_stack]		)
                    len_stack -= 1
              if visited.count(node[1]		) < 1:
                stack.append(node[1]		)
                visited.append(node[1]		)
                _snake_case						=							node[1]
                break

            # check if all the children are visited
          if s == ss:
            stack.pop()
            _snake_case						=							True
            if len(A__		) != 0:
              _snake_case						=							stack[len(A__		) - 1]
          else:
            _snake_case						=							False
            indirect_parents.append(A__		)
            _snake_case						=							s
            _snake_case						=							ss

          # check if se have reached the starting point
          if len(A__		) == 0:
            return list(A__		)



      def 						UpperCamelCase_		(      self				:    Optional[int]		)   ->					Optional[int]:
        _snake_case						=							[]
        _snake_case						=							[]
        _snake_case						=							list(self.graph		)[0]
        stack.append(A__		)
        visited.append(A__		)
        _snake_case						=							-2
        _snake_case						=							[]
        _snake_case						=							s
        _snake_case						=							False
        _snake_case						=							set()

        while True:
          # check if there is any non isolated nodes
          if len(self.graph[s]		) != 0:
            _snake_case						=							s
            for node in self.graph[s]:
              if (
                  visited.count(node[1]		) > 0
                  and node[1] != parent
                  and indirect_parents.count(node[1]		) > 0
                  and not on_the_way_back
              ):
                _snake_case						=							len(A__		) - 1
                while len_stack_minus_one >= 0:
                  if stack[len_stack_minus_one] == node[1]:
                    anticipating_nodes.add(node[1]		)
                    break
                  else:
                    return True
              if visited.count(node[1]		) < 1:
                stack.append(node[1]		)
                visited.append(node[1]		)
                _snake_case						=							node[1]
                break

            # check if all the children are visited
          if s == ss:
            stack.pop()
            _snake_case						=							True
            if len(A__		) != 0:
              _snake_case						=							stack[len(A__		) - 1]
          else:
            _snake_case						=							False
            indirect_parents.append(A__		)
            _snake_case						=							s
            _snake_case						=							ss

          # check if se have reached the starting point
          if len(A__		) == 0:
            return False



      def 						UpperCamelCase_		(      self				:    Union[str, Any]      , A__				:    List[str]=-2      , A__				:    int=-1		)   ->					Union[str, Any]:
        _snake_case						=							time()
        self.dfs(A__      , A__		)
        _snake_case						=							time()
        return end - begin







      def 						UpperCamelCase_		(      self				:    Optional[Any]      , A__				:    int=-2		)   ->					List[Any]:
        _snake_case						=							time()
        self.bfs(A__		)
        _snake_case						=							time()
        return end - begin



class   lowercase_   :



      def __init__(      self				:    Tuple		)   ->					Optional[int]:
        _snake_case						=							{}



      def 						UpperCamelCase_		(      self				:    Union[str, Any]      , A__				:    Union[str, Any]      , A__				:    int      , A__				:    List[str]=1		)   ->					Optional[Any]:
        # check if the u exists
        if self.graph.get(A__		):
          # if there already is a edge
          if self.graph[u].count([w, v]		) == 0:
            self.graph[u].append([w, v]		)
        else:
          # if u does not exist
          _snake_case						=							[[w, v]]
        # add the other way
        if self.graph.get(A__		):
          # if there already is a edge
          if self.graph[v].count([w, u]		) == 0:
            self.graph[v].append([w, u]		)
        else:
          # if u does not exist
          _snake_case						=							[[w, u]]



      def 						UpperCamelCase_		(      self				:    List[str]      , A__				:    Tuple      , A__				:    Optional[Any]		)   ->					List[Any]:
        if self.graph.get(A__		):
          for _ in self.graph[u]:
            if _[1] == v:
              self.graph[u].remove(A__		)
        # the other way round
        if self.graph.get(A__		):
          for _ in self.graph[v]:
            if _[1] == u:
              self.graph[v].remove(A__		)



      def 						UpperCamelCase_		(      self				:    List[Any]      , A__				:    int=-2      , A__				:    Optional[int]=-1		)   ->					Dict:
        if s == d:
          return []
        _snake_case						=							[]
        _snake_case						=							[]
        if s == -2:
          _snake_case						=							list(self.graph		)[0]
        stack.append(A__		)
        visited.append(A__		)
        _snake_case						=							s

        while True:
          # check if there is any non isolated nodes
          if len(self.graph[s]		) != 0:
            _snake_case						=							s
            for node in self.graph[s]:
              if visited.count(node[1]		) < 1:
                if node[1] == d:
                  visited.append(A__		)
                  return visited
                else:
                  stack.append(node[1]		)
                  visited.append(node[1]		)
                  _snake_case						=							node[1]
                  break

            # check if all the children are visited
          if s == ss:
            stack.pop()
            if len(A__		) != 0:
              _snake_case						=							stack[len(A__		) - 1]
          else:
            _snake_case						=							ss

          # check if se have reached the starting point
          if len(A__		) == 0:
            return visited



      def 						UpperCamelCase_		(      self				:    Optional[Any]      , A__				:    List[Any]=-1		)   ->					Union[str, Any]:
        if c == -1:
          _snake_case						=							floor(random() * 10000		) + 10
        for i in range(A__		):
          # every vertex has max 100 edges
          for _ in range(floor(random() * 102		) + 1		):
            _snake_case						=							floor(random() * c		) + 1
            if n != i:
              self.add_pair(A__      , A__      , 1		)



      def 						UpperCamelCase_		(      self				:    List[Any]      , A__				:    Any=-2		)   ->					str:
        _snake_case						=							deque()
        _snake_case						=							[]
        if s == -2:
          _snake_case						=							list(self.graph		)[0]
        d.append(A__		)
        visited.append(A__		)
        while d:
          _snake_case						=							d.popleft()
          if len(self.graph[s]		) != 0:
            for node in self.graph[s]:
              if visited.count(node[1]		) < 1:
                d.append(node[1]		)
                visited.append(node[1]		)
        return visited



      def 						UpperCamelCase_		(      self				:    Optional[int]      , A__				:    Optional[Any]		)   ->					List[Any]:
        return len(self.graph[u]		)



      def 						UpperCamelCase_		(      self				:    List[Any]		)   ->					Tuple:
        _snake_case						=							[]
        _snake_case						=							[]
        _snake_case						=							list(self.graph		)[0]
        stack.append(A__		)
        visited.append(A__		)
        _snake_case						=							-2
        _snake_case						=							[]
        _snake_case						=							s
        _snake_case						=							False
        _snake_case						=							set()

        while True:
          # check if there is any non isolated nodes
          if len(self.graph[s]		) != 0:
            _snake_case						=							s
            for node in self.graph[s]:
              if (
                  visited.count(node[1]		) > 0
                  and node[1] != parent
                  and indirect_parents.count(node[1]		) > 0
                  and not on_the_way_back
              ):
                _snake_case						=							len(A__		) - 1
                while len_stack >= 0:
                  if stack[len_stack] == node[1]:
                    anticipating_nodes.add(node[1]		)
                    break
                  else:
                    anticipating_nodes.add(stack[len_stack]		)
                    len_stack -= 1
              if visited.count(node[1]		) < 1:
                stack.append(node[1]		)
                visited.append(node[1]		)
                _snake_case						=							node[1]
                break

            # check if all the children are visited
          if s == ss:
            stack.pop()
            _snake_case						=							True
            if len(A__		) != 0:
              _snake_case						=							stack[len(A__		) - 1]
          else:
            _snake_case						=							False
            indirect_parents.append(A__		)
            _snake_case						=							s
            _snake_case						=							ss

          # check if se have reached the starting point
          if len(A__		) == 0:
            return list(A__		)



      def 						UpperCamelCase_		(      self				:    List[Any]		)   ->					List[Any]:
        _snake_case						=							[]
        _snake_case						=							[]
        _snake_case						=							list(self.graph		)[0]
        stack.append(A__		)
        visited.append(A__		)
        _snake_case						=							-2
        _snake_case						=							[]
        _snake_case						=							s
        _snake_case						=							False
        _snake_case						=							set()

        while True:
          # check if there is any non isolated nodes
          if len(self.graph[s]		) != 0:
            _snake_case						=							s
            for node in self.graph[s]:
              if (
                  visited.count(node[1]		) > 0
                  and node[1] != parent
                  and indirect_parents.count(node[1]		) > 0
                  and not on_the_way_back
              ):
                _snake_case						=							len(A__		) - 1
                while len_stack_minus_one >= 0:
                  if stack[len_stack_minus_one] == node[1]:
                    anticipating_nodes.add(node[1]		)
                    break
                  else:
                    return True
              if visited.count(node[1]		) < 1:
                stack.append(node[1]		)
                visited.append(node[1]		)
                _snake_case						=							node[1]
                break

            # check if all the children are visited
          if s == ss:
            stack.pop()
            _snake_case						=							True
            if len(A__		) != 0:
              _snake_case						=							stack[len(A__		) - 1]
          else:
            _snake_case						=							False
            indirect_parents.append(A__		)
            _snake_case						=							s
            _snake_case						=							ss

          # check if se have reached the starting point
          if len(A__		) == 0:
            return False



      def 						UpperCamelCase_		(      self				:    Optional[int]		)   ->					str:
        return list(self.graph		)



      def 						UpperCamelCase_		(      self				:    List[Any]      , A__				:    str=-2      , A__				:    List[Any]=-1		)   ->					List[str]:
        _snake_case						=							time()
        self.dfs(A__      , A__		)
        _snake_case						=							time()
        return end - begin







      def 						UpperCamelCase_		(      self				:    Union[str, Any]      , A__				:    int=-2		)   ->					Dict:
        _snake_case						=							time()
        self.bfs(A__		)
        _snake_case						=							time()
        return end - begin




import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_video_inputs


if is_torch_available():
 import torch

if is_vision_available():
 from PIL import Image

 from transformers import VivitImageProcessor



class   lowercase_   (  unittest.TestCase					):



      def __init__(      self				:    Union[str, Any]      , A__				:    int      , A__				:    List[str]=7      , A__				:    Tuple=3      , A__				:    List[str]=10      , A__				:    Optional[int]=18      , A__				:    int=30      , A__				:    Tuple=400      , A__				:    Dict=True      , A__				:    str=None      , A__				:    str=True      , A__				:    List[str]=[0.5, 0.5, 0.5]      , A__				:    int=[0.5, 0.5, 0.5]      , A__				:    List[Any]=None      , )   ->					int:
        _snake_case						=							size if size is not None else {'''shortest_edge''': 18}
        _snake_case						=							crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}

        _snake_case						=							parent
        _snake_case						=							batch_size
        _snake_case						=							num_channels
        _snake_case						=							num_frames
        _snake_case						=							image_size
        _snake_case						=							min_resolution
        _snake_case						=							max_resolution
        _snake_case						=							do_resize
        _snake_case						=							size
        _snake_case						=							do_normalize
        _snake_case						=							image_mean
        _snake_case						=							image_std
        _snake_case						=							crop_size







      def 						UpperCamelCase_		(      self				:    List[str]		)   ->					Union[str, Any]:
        return {
            "image_mean": self.image_mean,
            "image_std": self.image_std,
            "do_normalize": self.do_normalize,
            "do_resize": self.do_resize,
            "size": self.size,
            "crop_size": self.crop_size,
        }



@require_torch
@require_vision
class   lowercase_   (  __lowercase       ,							unittest.TestCase					):
      UpperCamelCase_      :		Tuple										=    VivitImageProcessor if is_vision_available() else None



      def 						UpperCamelCase_		(      self				:    Union[str, Any]		)   ->					List[str]:
        _snake_case						=							VivitImageProcessingTester(self		)



      @property
      def 						UpperCamelCase_		(      self				:    Union[str, Any]		)   ->					Optional[Any]:
        return self.image_processor_tester.prepare_image_processor_dict()



      def 						UpperCamelCase_		(      self				:    Optional[int]		)   ->					Optional[Any]:
        _snake_case						=							self.image_processing_class(**self.image_processor_dict		)
        self.assertTrue(hasattr(A__      , '''image_mean'''		)		)
        self.assertTrue(hasattr(A__      , '''image_std'''		)		)
        self.assertTrue(hasattr(A__      , '''do_normalize'''		)		)
        self.assertTrue(hasattr(A__      , '''do_resize'''		)		)
        self.assertTrue(hasattr(A__      , '''do_center_crop'''		)		)
        self.assertTrue(hasattr(A__      , '''size'''		)		)



      def 						UpperCamelCase_		(      self				:    int		)   ->					List[Any]:
        _snake_case						=							self.image_processing_class.from_dict(self.image_processor_dict		)
        self.assertEqual(image_processor.size      , {'''shortest_edge''': 18}		)
        self.assertEqual(image_processor.crop_size      , {'''height''': 18, '''width''': 18}		)

        _snake_case						=							self.image_processing_class.from_dict(self.image_processor_dict      , size=42      , crop_size=84		)
        self.assertEqual(image_processor.size      , {'''shortest_edge''': 42}		)
        self.assertEqual(image_processor.crop_size      , {'''height''': 84, '''width''': 84}		)



      def 						UpperCamelCase_		(      self				:    List[Any]		)   ->					Optional[Any]:
        # Initialize image_processing
        _snake_case						=							self.image_processing_class(**self.image_processor_dict		)
        # create random PIL videos
        _snake_case						=							prepare_video_inputs(self.image_processor_tester      , equal_resolution=A__		)
        for video in video_inputs:
          self.assertIsInstance(A__      , A__		)
          self.assertIsInstance(video[0]      , Image.Image		)

        # Test not batched input
        _snake_case						=							image_processing(video_inputs[0]      , return_tensors='''pt'''		).pixel_values
        self.assertEqual(
            encoded_videos.shape      , (
                1,
                self.image_processor_tester.num_frames,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )      , )

        # Test batched
        _snake_case						=							image_processing(A__      , return_tensors='''pt'''		).pixel_values
        self.assertEqual(
            encoded_videos.shape      , (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_frames,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )      , )



      def 						UpperCamelCase_		(      self				:    Any		)   ->					List[str]:
        # Initialize image_processing
        _snake_case						=							self.image_processing_class(**self.image_processor_dict		)
        # create random numpy tensors
        _snake_case						=							prepare_video_inputs(self.image_processor_tester      , equal_resolution=A__      , numpify=A__		)
        for video in video_inputs:
          self.assertIsInstance(A__      , A__		)
          self.assertIsInstance(video[0]      , np.ndarray		)

        # Test not batched input
        _snake_case						=							image_processing(video_inputs[0]      , return_tensors='''pt'''		).pixel_values
        self.assertEqual(
            encoded_videos.shape      , (
                1,
                self.image_processor_tester.num_frames,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )      , )

        # Test batched
        _snake_case						=							image_processing(A__      , return_tensors='''pt'''		).pixel_values
        self.assertEqual(
            encoded_videos.shape      , (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_frames,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )      , )







      def 						UpperCamelCase_		(      self				:    Optional[Any]		)   ->					int:
        # Initialize image_processing
        _snake_case						=							self.image_processing_class(**self.image_processor_dict		)
        # create random PyTorch tensors
        _snake_case						=							prepare_video_inputs(self.image_processor_tester      , equal_resolution=A__      , torchify=A__		)
        for video in video_inputs:
          self.assertIsInstance(A__      , A__		)
          self.assertIsInstance(video[0]      , torch.Tensor		)

        # Test not batched input
        _snake_case						=							image_processing(video_inputs[0]      , return_tensors='''pt'''		).pixel_values
        self.assertEqual(
            encoded_videos.shape      , (
                1,
                self.image_processor_tester.num_frames,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )      , )

        # Test batched
        _snake_case						=							image_processing(A__      , return_tensors='''pt'''		).pixel_values
        self.assertEqual(
            encoded_videos.shape      , (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_frames,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )      , )

