

"""simple docstring"""





_a							=							{
    'A': ['B', 'C', 'E'],
    'B': ['A', 'D', 'E'],
    'C': ['A', 'F', 'G'],
    'D': ['B'],
    'E': ['A', 'B', 'D'],
    'F': ['C'],
    'G': ['C'],
}




def   _A  (  UpperCamelCase_					:	dict,      UpperCamelCase_					:	List[str],      UpperCamelCase_					:	int)     ->						list[str]:
   '''simple docstring'''


   __lowercase								=						set()
   # keep track of all the paths to be checked
   __lowercase								=						[[start]]

   # return path if start is goal
   if start == goal:
      return [start]

   # keeps looping until all possible paths have been checked
   while queue:
      # pop the first path from the queue
      __lowercase								=						queue.pop(0)
      # get the last node from the path
      __lowercase								=						path[-1]
      if node not in explored:
         __lowercase								=						graph[node]
         # go through all neighbour nodes, construct a new path and
         # push it into the queue
         for neighbour in neighbours:
            __lowercase								=						list(UpperCamelCase_)
            new_path.append(UpperCamelCase_)
            queue.append(UpperCamelCase_)
            # return path if neighbour is goal
            if neighbour == goal:
               return new_path

            # mark node as explored
         explored.add(UpperCamelCase_)

    # in case there's no path between the 2 nodes
   return []




def   _A  (  UpperCamelCase_					:	dict,      UpperCamelCase_					:	Any,      UpperCamelCase_					:	Any)     ->						int:
   '''simple docstring'''


   if not graph or start not in graph or target not in graph:
      return -1
   if start == target:
      return 0
   __lowercase								=						[start]
   __lowercase								=						set(UpperCamelCase_)
   # Keep tab on distances from `start` node.
   __lowercase								=						{start: 0, target: -1}
   while queue:
      __lowercase								=						queue.pop(0)
      if node == target:
         __lowercase								=						(
             dist[node] if dist[target] == -1 else min(dist[target],      dist[node])
         )
      for adjacent in graph[node]:
         if adjacent not in visited:
            visited.add(UpperCamelCase_)
            queue.append(UpperCamelCase_)
            __lowercase								=						dist[node] + 1
   return dist[target]


if __name__ == "__main__":
   print(bfs_shortest_path(demo_graph, 'G', 'D'))  # returns ['G', 'C', 'A', 'B', 'D']
   print(bfs_shortest_path_distance(demo_graph, 'G', 'D'))  # returns 4







"""simple docstring"""





from __future__ import annotations

from typing import Any




class  _lowerCAmelCase  :







 """simple docstring"""







 def __init__(      self							:      Union[str, Any], UpperCAmelCase__							:      int     ):
    __lowercase								=						num_of_nodes
    __lowercase								=						[]
    __lowercase								=						{}


 def  _lowercase			(      self							:      Tuple, UpperCAmelCase__							:      int, UpperCAmelCase__							:      int, UpperCAmelCase__							:      int     ):
    self.m_edges.append([u_node, v_node, weight]     )


 def  _lowercase			(      self							:      Dict, UpperCAmelCase__							:      int     ):

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


 def  _lowercase			(      self							:      Dict, UpperCAmelCase__							:      int     ):

    if self.m_component[u_node] != u_node:
       for k in self.m_component:
          __lowercase								=						self.find_component(UpperCAmelCase__     )


 def  _lowercase			(      self							:      str, UpperCAmelCase__							:      list[int], UpperCAmelCase__							:      int, UpperCAmelCase__							:      int     ):

    if component_size[u_node] <= component_size[v_node]:
       __lowercase								=						v_node
       component_size[v_node] += component_size[u_node]
       self.set_component(UpperCAmelCase__     )

    elif component_size[u_node] >= component_size[v_node]:
       __lowercase								=						self.find_component(UpperCAmelCase__     )
       component_size[u_node] += component_size[v_node]
       self.set_component(UpperCAmelCase__     )



 def  _lowercase			(      self							:      Tuple     ):
    __lowercase								=						[]
    __lowercase								=						0

    __lowercase								=						[-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     )

    __lowercase								=						self.m_num_of_nodes

    while num_of_components > 1:
       for edge in self.m_edges:
          __lowercase		,__lowercase		,__lowercase								=						edge

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

       for edge in minimum_weight_edge:
          if isinstance(UpperCAmelCase__, UpperCAmelCase__     ):
             __lowercase		,__lowercase		,__lowercase								=						edge

             __lowercase								=						self.m_component[u]
             __lowercase								=						self.m_component[v]

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

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







def   _A  (  )     ->						None:
   '''simple docstring'''




if __name__ == "__main__":
   import doctest

   doctest.testmod()




