




'''simple docstring'''
import sys
from collections.abc import Mapping

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter






class 							lowercase__			(							TensorFormatter[Mapping, np.ndarray, Mapping]     ):





	def __init__(				self      :       Union[str, Any]				,lowerCamelCase__      :       Union[str, Any]=None				,**lowerCamelCase__      :       List[Any]				):







							'''simple docstring'''
							super().__init__(features=lowerCamelCase__				)
							_UpperCamelCase      :	Dict				       =     np_array_kwargs





	def 			UpperCamelCase_						(				self      :       Tuple				,lowerCamelCase__      :       str				):







							'''simple docstring'''






							if isinstance(lowerCamelCase__				,lowerCamelCase__				):
													if column and all(
													    isinstance(lowerCamelCase__				,np.ndarray				) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column				):
																			return np.stack(lowerCamelCase__				)
													else:
																			# don't use np.array(column, dtype=object)
																			# since it fails in certain cases
																			# see https://stackoverflow.com/q/51005699
																			_UpperCamelCase      :	Any				       =     np.empty(len(lowerCamelCase__				)				,dtype=lowerCamelCase__				)
																			_UpperCamelCase      :	Any				       =     column
																			return out
							return column





	def 			UpperCamelCase_						(				self      :       Any				,lowerCamelCase__      :       List[str]				):







							'''simple docstring'''






							if isinstance(lowerCamelCase__				,(str, bytes, type(lowerCamelCase__				))				):
													return value
							elif isinstance(lowerCamelCase__				,(np.character, np.ndarray)				) and np.issubdtype(value.dtype				,np.character				):
													return value
							elif isinstance(lowerCamelCase__				,np.number				):
													return value

							_UpperCamelCase      :	Optional[Any]				       =     {}

							if isinstance(lowerCamelCase__				,np.ndarray				) and np.issubdtype(value.dtype				,np.integer				):
													_UpperCamelCase      :	List[str]				       =     {'dtype': np.intaa}
							elif isinstance(lowerCamelCase__				,np.ndarray				) and np.issubdtype(value.dtype				,np.floating				):
													_UpperCamelCase      :	Optional[Any]				       =     {'dtype': np.floataa}
							elif config.PIL_AVAILABLE and "PIL" in sys.modules:
													import PIL.Image

													if isinstance(lowerCamelCase__				,PIL.Image.Image				):
																			return np.asarray(lowerCamelCase__				,**self.np_array_kwargs				)

							return np.asarray(lowerCamelCase__				,**{**default_dtype, **self.np_array_kwargs}				)





	def 			UpperCamelCase_						(				self      :       int				,lowerCamelCase__      :       List[Any]				):







							'''simple docstring'''






							if config.TORCH_AVAILABLE and "torch" in sys.modules:
													import torch

													if isinstance(lowerCamelCase__				,torch.Tensor				):
																			return self._tensorize(data_struct.detach().cpu().numpy()[()]				)
							if hasattr(lowerCamelCase__				,'__array__'				) and not isinstance(lowerCamelCase__				,(np.ndarray, np.character, np.number)				):
													_UpperCamelCase      :	Dict				       =     data_struct.__array__()
							# support for nested types like struct of list of struct
							if isinstance(lowerCamelCase__				,np.ndarray				):
													if data_struct.dtype == object:
																			return self._consolidate([self.recursive_tensorize(lowerCamelCase__				) for substruct in data_struct]				)
							if isinstance(lowerCamelCase__				,(list, tuple)				):
													return self._consolidate([self.recursive_tensorize(lowerCamelCase__				) for substruct in data_struct]				)
							return self._tensorize(lowerCamelCase__				)





	def 			UpperCamelCase_						(				self      :       Dict				,lowerCamelCase__      :       Any				):







							'''simple docstring'''
							return map_nested(self._recursive_tensorize				,lowerCamelCase__				,map_list=lowerCamelCase__				)





	def 			UpperCamelCase_						(				self      :       Tuple				,lowerCamelCase__      :       List[Any]				):







							'''simple docstring'''
							_UpperCamelCase      :	str				       =     self.numpy_arrow_extractor().extract_row(lowerCamelCase__				)
							_UpperCamelCase      :	Any				       =     self.python_features_decoder.decode_row(lowerCamelCase__				)
							return self.recursive_tensorize(lowerCamelCase__				)





	def 			UpperCamelCase_						(				self      :       List[str]				,lowerCamelCase__      :       str				):







							'''simple docstring'''
							_UpperCamelCase      :	List[Any]				       =     self.numpy_arrow_extractor().extract_column(lowerCamelCase__				)
							_UpperCamelCase      :	Optional[Any]				       =     self.python_features_decoder.decode_column(lowerCamelCase__				,pa_table.column_names[0]				)
							_UpperCamelCase      :	str				       =     self.recursive_tensorize(lowerCamelCase__				)
							_UpperCamelCase      :	List[str]				       =     self._consolidate(lowerCamelCase__				)
							return column





	def 			UpperCamelCase_						(				self      :       List[Any]				,lowerCamelCase__      :       List[Any]				):







							'''simple docstring'''
							_UpperCamelCase      :	Tuple				       =     self.numpy_arrow_extractor().extract_batch(lowerCamelCase__				)
							_UpperCamelCase      :	List[Any]				       =     self.python_features_decoder.decode_batch(lowerCamelCase__				)
							_UpperCamelCase      :	Dict				       =     self.recursive_tensorize(lowerCamelCase__				)
							for column_name in batch:
													_UpperCamelCase      :	Union[str, Any]				       =     self._consolidate(batch[column_name]				)
							return batch







'''simple docstring'''
from __future__ import annotations

from queue import Queue






class 							lowercase__			:





	def __init__(				self      :       int				):







							'''simple docstring'''
							_UpperCamelCase      :	dict[int, list[int]]				       =     {}





	def 			UpperCamelCase_						(				self      :       int				):







							'''simple docstring'''
							for i in self.vertices:
													print(lowerCamelCase__				,' : '				,' -> '.join([str(lowerCamelCase__				) for j in self.vertices[i]]				)				)





	def 			UpperCamelCase_						(				self      :       Optional[Any]				,lowerCamelCase__      :       int				,lowerCamelCase__      :       int				):







							'''simple docstring'''
							if from_vertex in self.vertices:
													self.vertices[from_vertex].append(lowerCamelCase__				)
							else:
													_UpperCamelCase      :	Any				       =     [to_vertex]





	def 			UpperCamelCase_						(				self      :       int				,lowerCamelCase__      :       int				):







							'''simple docstring'''
							_UpperCamelCase      :	Dict				       =     set()

							# create a first in first out queue to store all the vertices for BFS
							_UpperCamelCase      :	Queue				       =     Queue()

							# mark the source node as visited and enqueue it
							visited.add(lowerCamelCase__				)
							queue.put(lowerCamelCase__				)

							while not queue.empty():
													_UpperCamelCase      :	Optional[Any]				       =     queue.get()

													# loop through all adjacent vertex and enqueue it if not yet visited
													for adjacent_vertex in self.vertices[vertex]:
																			if adjacent_vertex not in visited:
																									queue.put(lowerCamelCase__				)
																									visited.add(lowerCamelCase__				)
							return visited


if __name__ == "__main__":
					from doctest import testmod

					testmod(verbose=True)

					snake_case_				:			int						 =						Graph()
					g.add_edge(0, 1)
					g.add_edge(0, 2)
					g.add_edge(1, 2)
					g.add_edge(2, 0)
					g.add_edge(2, 3)
					g.add_edge(3, 3)

					g.print_graph()
					# 0  :  1 -> 2
					# 1  :  2
					# 2  :  0 -> 3
					# 3  :  3

					assert sorted(g.bfs(2)) == [0, 1, 2, 3]


