




import math
class 			a__    :






						def 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A			):




											"""simple docstring"""



											__lowerCAmelCase				   =       0.0
											__lowerCAmelCase				   =       0.0
											for i in range(len(_A			)			):
																da += math.pow((sample[i] - weights[0][i])		,			2			)
																da += math.pow((sample[i] - weights[1][i])		,			2			)
																return 0 if da > da else 1
											return 0






						def 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A		,			_A		,			_A			):




											"""simple docstring"""



											for i in range(len(_A			)			):
																weights[j][i] += alpha * (sample[i] - weights[j][i])
											return weights







def 	_a      (						):
					# Training Examples ( m, n )
					__lowerCAmelCase				   =       [[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]]

					# weight initialization ( n, C )
					__lowerCAmelCase				   =       [[0.2, 0.6, 0.5, 0.9], [0.8, 0.4, 0.7, 0.3]]

					# training
					__lowerCAmelCase				   =       SelfOrganizingMap()
					__lowerCAmelCase				   =       3
					__lowerCAmelCase				   =       0.5

					for _ in range(SCREAMING_SNAKE_CASE_     ):
										for j in range(len(SCREAMING_SNAKE_CASE_     )     ):
															# training sample
															__lowerCAmelCase				   =       training_samples[j]

															# Compute the winning vector
															__lowerCAmelCase				   =       self_organizing_map.get_winner(SCREAMING_SNAKE_CASE_      ,      SCREAMING_SNAKE_CASE_     )

															# Update the winning vector
															__lowerCAmelCase				   =       self_organizing_map.update(SCREAMING_SNAKE_CASE_      ,      SCREAMING_SNAKE_CASE_      ,      SCREAMING_SNAKE_CASE_      ,      SCREAMING_SNAKE_CASE_     )

    # classify test sample
					__lowerCAmelCase				   =       [0, 0, 0, 1]
					__lowerCAmelCase				   =       self_organizing_map.get_winner(SCREAMING_SNAKE_CASE_      ,      SCREAMING_SNAKE_CASE_     )

					# results
					print(F"""Clusters that the test sample belongs to : {winner}"""     )
					print(F"""Weights that have been trained : {weights}"""     )


# running the main() function
if __name__ == "__main__":
					main()










from queue import PriorityQueue
from typing import Any

import numpy as np







def 	_a      (						SCREAMING_SNAKE_CASE_							:				dict      ,      SCREAMING_SNAKE_CASE_							:				str      ,      SCREAMING_SNAKE_CASE_							:				set      ,      SCREAMING_SNAKE_CASE_							:				set      ,      SCREAMING_SNAKE_CASE_							:				dict      ,      SCREAMING_SNAKE_CASE_							:				dict      ,      SCREAMING_SNAKE_CASE_							:				PriorityQueue      ,      SCREAMING_SNAKE_CASE_							:				dict      ,      SCREAMING_SNAKE_CASE_							:				float | int      ,      ):
					for nxt, d in graph[v]:
										if nxt in visited_forward:
															continue
										__lowerCAmelCase				   =       cst_fwd.get(SCREAMING_SNAKE_CASE_      ,      np.inf     )
										__lowerCAmelCase				   =       cst_fwd[v] + d
										if new_cost_f < old_cost_f:
															queue.put((new_cost_f, nxt)     )
															__lowerCAmelCase				   =       new_cost_f
															__lowerCAmelCase				   =       v
										if nxt in visited_backward:
															if cst_fwd[v] + d + cst_bwd[nxt] < shortest_distance:
																				__lowerCAmelCase				   =       cst_fwd[v] + d + cst_bwd[nxt]
					return shortest_distance







def 	_a      (						SCREAMING_SNAKE_CASE_							:				str      ,      SCREAMING_SNAKE_CASE_							:				str      ,      SCREAMING_SNAKE_CASE_							:				dict      ,      SCREAMING_SNAKE_CASE_							:				dict     ):
					__lowerCAmelCase				   =       -1

					__lowerCAmelCase				   =       set()
					__lowerCAmelCase				   =       set()
					__lowerCAmelCase				   =       {source: 0}
					__lowerCAmelCase				   =       {destination: 0}
					__lowerCAmelCase				   =       {source: None}
					__lowerCAmelCase				   =       {destination: None}
					__lowerCAmelCase				   =       PriorityQueue()
					__lowerCAmelCase				   =       PriorityQueue()

					__lowerCAmelCase				   =       np.inf

					queue_forward.put((0, source)     )
					queue_backward.put((0, destination)     )

					if source == destination:
										return 0

					while not queue_forward.empty() and not queue_backward.empty():
										__lowerCAmelCase   ,							__lowerCAmelCase				   =       queue_forward.get()
										visited_forward.add(SCREAMING_SNAKE_CASE_     )

										__lowerCAmelCase   ,							__lowerCAmelCase				   =       queue_backward.get()
										visited_backward.add(SCREAMING_SNAKE_CASE_     )

										__lowerCAmelCase				   =       pass_and_relaxation(
										    SCREAMING_SNAKE_CASE_      ,      SCREAMING_SNAKE_CASE_      ,      SCREAMING_SNAKE_CASE_      ,      SCREAMING_SNAKE_CASE_      ,      SCREAMING_SNAKE_CASE_      ,      SCREAMING_SNAKE_CASE_      ,      SCREAMING_SNAKE_CASE_      ,      SCREAMING_SNAKE_CASE_      ,      SCREAMING_SNAKE_CASE_      ,      )

										__lowerCAmelCase				   =       pass_and_relaxation(
										    SCREAMING_SNAKE_CASE_      ,      SCREAMING_SNAKE_CASE_      ,      SCREAMING_SNAKE_CASE_      ,      SCREAMING_SNAKE_CASE_      ,      SCREAMING_SNAKE_CASE_      ,      SCREAMING_SNAKE_CASE_      ,      SCREAMING_SNAKE_CASE_      ,      SCREAMING_SNAKE_CASE_      ,      SCREAMING_SNAKE_CASE_      ,      )

										if cst_fwd[v_fwd] + cst_bwd[v_bwd] >= shortest_distance:
															break

					if shortest_distance != np.inf:
										__lowerCAmelCase				   =       shortest_distance
					return shortest_path_distance


UpperCamelCase__    			=			{
    """B""": [["""C""", 1]],
    """C""": [["""D""", 1]],
    """D""": [["""F""", 1]],
    """E""": [["""B""", 1], ["""G""", 2]],
    """F""": [],
    """G""": [["""F""", 1]],
}
UpperCamelCase__    			=			{
    """B""": [["""E""", 1]],
    """C""": [["""B""", 1]],
    """D""": [["""C""", 1]],
    """F""": [["""D""", 1], ["""G""", 1]],
    """E""": [[None, np.inf]],
    """G""": [["""E""", 2]],
}

if __name__ == "__main__":
					import doctest

					doctest.testmod()




