


from __future__ import annotations

from sys import maxsize
from typing import Generic, TypeVar

lowerCamelCase						=		TypeVar('''T''')






def 					lowerCamelCase_   (	_a	):




		"""simple docstring"""






		return (position - 1) // 2






def 					lowerCamelCase_   (	_a	):




		"""simple docstring"""






		return (2 * position) + 1






def 					lowerCamelCase_   (	_a	):




		"""simple docstring"""






		return (2 * position) + 2





class  _a							(    Generic[T]):




	def __init__( self						:		Dict		)->	None:
			lowerCAmelCase__       :      list[tuple[T, int]]   					= []
			lowerCAmelCase__       :      dict[T, int]   					= {}
			lowerCAmelCase__       :      int   					= 0




	def __len__( self						:		Any		)->	int:
			return self.elements




	def __repr__( self						:		Optional[int]		)->	str:
			return str(self.heap		)




	def 							UpperCAmelCase__( self						:		List[str]		)->	bool:
			# Check if the priority queue is empty
			return self.elements == 0




	def 							UpperCAmelCase__( self						:		Any		, _SCREAMING_SNAKE_CASE						:		T		, _SCREAMING_SNAKE_CASE						:		int		)->	None:
			# Add an element with given priority to the queue
			self.heap.append((elem, weight)		)
			lowerCAmelCase__       :      str   					= self.elements
			self.elements += 1
			self._bubble_up(_SCREAMING_SNAKE_CASE		)




	def 							UpperCAmelCase__( self						:		Optional[Any]		)->	T:
			# Remove and return the element with lowest weight (highest priority)
			if self.elements > 1:
					self._swap_nodes(0		, self.elements - 1		)
			lowerCAmelCase__       :      Any   					= self.heap.pop()
			del self.position_map[elem]
			self.elements -= 1
			if self.elements > 0:
					lowerCAmelCase__       :      str   					= self.heap[0]
					self._bubble_down(_SCREAMING_SNAKE_CASE		)
			return elem




	def 							UpperCAmelCase__( self						:		int		, _SCREAMING_SNAKE_CASE						:		T		, _SCREAMING_SNAKE_CASE						:		int		)->	None:
			# Update the weight of the given key
			lowerCAmelCase__       :      Union[str, Any]   					= self.position_map[elem]
			lowerCAmelCase__       :      List[Any]   					= (elem, weight)
			if position > 0:
					lowerCAmelCase__       :      str   					= get_parent_position(_SCREAMING_SNAKE_CASE		)
					lowerCAmelCase__       :      Tuple   					= self.heap[parent_position]
					if parent_weight > weight:
							self._bubble_up(_SCREAMING_SNAKE_CASE		)
					else:
							self._bubble_down(_SCREAMING_SNAKE_CASE		)
			else:
					self._bubble_down(_SCREAMING_SNAKE_CASE		)




	def 							UpperCAmelCase__( self						:		Dict		, _SCREAMING_SNAKE_CASE						:		T		)->	None:
			# Place a node at the proper position (upward movement) [to be used internally
			# only]
			lowerCAmelCase__       :      str   					= self.position_map[elem]
			if curr_pos == 0:
					return None
			lowerCAmelCase__       :      Any   					= get_parent_position(_SCREAMING_SNAKE_CASE		)
			lowerCAmelCase__       :      Dict   					= self.heap[curr_pos]
			lowerCAmelCase__       :      str   					= self.heap[parent_position]
			if parent_weight > weight:
					self._swap_nodes(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		)
					return self._bubble_up(_SCREAMING_SNAKE_CASE		)
			return None




	def 							UpperCAmelCase__( self						:		Any		, _SCREAMING_SNAKE_CASE						:		T		)->	None:
			# Place a node at the proper position (downward movement) [to be used
			# internally only]
			lowerCAmelCase__       :      List[Any]   					= self.position_map[elem]
			lowerCAmelCase__       :      List[Any]   					= self.heap[curr_pos]
			lowerCAmelCase__       :      Union[str, Any]   					= get_child_left_position(_SCREAMING_SNAKE_CASE		)
			lowerCAmelCase__       :      Optional[int]   					= get_child_right_position(_SCREAMING_SNAKE_CASE		)
			if child_left_position < self.elements and child_right_position < self.elements:
					lowerCAmelCase__       :      Optional[Any]   					= self.heap[child_left_position]
					lowerCAmelCase__       :      Dict   					= self.heap[child_right_position]
					if child_right_weight < child_left_weight and child_right_weight < weight:
							self._swap_nodes(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		)
							return self._bubble_down(_SCREAMING_SNAKE_CASE		)
			if child_left_position < self.elements:
					lowerCAmelCase__       :      Tuple   					= self.heap[child_left_position]
					if child_left_weight < weight:
							self._swap_nodes(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		)
							return self._bubble_down(_SCREAMING_SNAKE_CASE		)
			else:
					return None
			if child_right_position < self.elements:
					lowerCAmelCase__       :      List[str]   					= self.heap[child_right_position]
					if child_right_weight < weight:
							self._swap_nodes(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		)
							return self._bubble_down(_SCREAMING_SNAKE_CASE		)
			return None




	def 							UpperCAmelCase__( self						:		List[str]		, _SCREAMING_SNAKE_CASE						:		int		, _SCREAMING_SNAKE_CASE						:		int		)->	None:
			# Swap the nodes at the given positions
			lowerCAmelCase__       :      str   					= self.heap[nodea_pos][0]
			lowerCAmelCase__       :      List[str]   					= self.heap[nodea_pos][0]
			lowerCAmelCase__       :      Dict   					= (
			    self.heap[nodea_pos],
			    self.heap[nodea_pos],
			)
			lowerCAmelCase__       :      str   					= nodea_pos
			lowerCAmelCase__       :      int   					= nodea_pos





class  _a							(    Generic[T]):




	def __init__( self						:		Dict		)->	None:
			lowerCAmelCase__       :      dict[T, dict[T, int]]   					= {}
			lowerCAmelCase__       :      int   					= 0




	def __repr__( self						:		str		)->	str:
			return str(self.connections		)




	def __len__( self						:		List[Any]		)->	int:
			return self.nodes




	def 							UpperCAmelCase__( self						:		List[str]		, _SCREAMING_SNAKE_CASE						:		T		)->	None:
			# Add a node in the graph if it is not in the graph
			if node not in self.connections:
					lowerCAmelCase__       :      Optional[int]   					= {}
					self.nodes += 1




	def 							UpperCAmelCase__( self						:		Any		, _SCREAMING_SNAKE_CASE						:		T		, _SCREAMING_SNAKE_CASE						:		T		, _SCREAMING_SNAKE_CASE						:		int		)->	None:
			# Add an edge between 2 nodes in the graph
			self.add_node(_SCREAMING_SNAKE_CASE		)
			self.add_node(_SCREAMING_SNAKE_CASE		)
			lowerCAmelCase__       :      str   					= weight
			lowerCAmelCase__       :      List[Any]   					= weight






def 					lowerCamelCase_   (	_a     ,       ):




		"""simple docstring"""






		lowerCAmelCase__       :      dict[T, int]   					= {node: maxsize for node in graph.connections}
		lowerCAmelCase__       :      dict[T, T | None]   					= {node: None for node in graph.connections}

		lowerCAmelCase__       :      MinPriorityQueue[T]   					= MinPriorityQueue()
		for node, weight in dist.items():
				priority_queue.push(_a     ,       _a	)

		if priority_queue.is_empty():
				return dist, parent

		# initialization
		lowerCAmelCase__       :      Union[str, Any]   					= priority_queue.extract_min()
		lowerCAmelCase__       :      Dict   					= 0
		for neighbour in graph.connections[node]:
				if dist[neighbour] > dist[node] + graph.connections[node][neighbour]:
						lowerCAmelCase__       :      Optional[int]   					= dist[node] + graph.connections[node][neighbour]
						priority_queue.update_key(_a     ,       dist[neighbour]	)
						lowerCAmelCase__       :      Union[str, Any]   					= node

    # running prim's algorithm
		while not priority_queue.is_empty():
				lowerCAmelCase__       :      str   					= priority_queue.extract_min()
				for neighbour in graph.connections[node]:
						if dist[neighbour] > dist[node] + graph.connections[node][neighbour]:
								lowerCAmelCase__       :      List[str]   					= dist[node] + graph.connections[node][neighbour]
								priority_queue.update_key(_a     ,       dist[neighbour]	)
								lowerCAmelCase__       :      Any   					= node
		return dist, parent








import timeit

import numpy as np

import datasets
from datasets.arrow_writer import ArrowWriter
from datasets.features.features import _ArrayXD






def 					lowerCamelCase_   (	_a	):




		"""simple docstring"""






		def wrapper(*_a     ,       **_a	):
				lowerCAmelCase__       :      List[str]   					= timeit.default_timer()
				lowerCAmelCase__       :      List[Any]   					= func(*_a     ,       **_a	)
				lowerCAmelCase__       :      Any   					= timeit.default_timer() - starttime
				return delta

		lowerCAmelCase__       :      Any   					= func.__name__

		return wrapper






def 					lowerCamelCase_   (	_a     ,       _a=100     ,       _a=None	):




		"""simple docstring"""






		lowerCAmelCase__       :      str   					= []
		lowerCAmelCase__       :      str   					= seq_shapes or {}
		for i in range(_a	):
				lowerCAmelCase__       :      List[str]   					= {}
				for col_id, (k, v) in enumerate(features.items()	):
						if isinstance(_a     ,       _ArrayXD	):
								lowerCAmelCase__       :      List[str]   					= np.random.rand(*v.shape	).astype(v.dtype	)
						elif isinstance(_a     ,       datasets.Value	):
								if v.dtype == "string":
										lowerCAmelCase__       :      Dict   					= '''The small grey turtle was surprisingly fast when challenged.'''
								else:
										lowerCAmelCase__       :      Any   					= np.random.randint(10     ,       size=1	).astype(v.dtype	).item()
						elif isinstance(_a     ,       datasets.Sequence	):
								while isinstance(_a     ,       datasets.Sequence	):
										lowerCAmelCase__       :      Optional[int]   					= v.feature
								lowerCAmelCase__       :      str   					= seq_shapes[k]
								lowerCAmelCase__       :      Any   					= np.random.rand(*_a	).astype(v.dtype	)
						lowerCAmelCase__       :      int   					= data

				dummy_data.append((i, example)	)

		return dummy_data






def 					lowerCamelCase_   (	_a     ,       _a     ,       _a=100     ,       _a=None	):




		"""simple docstring"""






		lowerCAmelCase__       :      Optional[Any]   					= generate_examples(_a     ,       num_examples=_a     ,       seq_shapes=_a	)

		with ArrowWriter(features=_a     ,       path=_a	) as writer:
				for key, record in dummy_data:
						lowerCAmelCase__       :      Optional[int]   					= features.encode_example(_a	)
						writer.write(_a	)

				lowerCAmelCase__					,     lowerCAmelCase__       :      List[str]   					= writer.finalize()

		if not num_final_examples == num_examples:
				raise ValueError(
				    f'Error writing the dataset, wrote {num_final_examples} examples but should have written {num_examples}.'	)

		lowerCAmelCase__       :      List[Any]   					= datasets.Dataset.from_file(filename=_a     ,       info=datasets.DatasetInfo(features=_a	)	)

		return dataset




