<p>There is a <strong>bi-directional </strong>graph with <code>n</code> vertices, where each vertex is labeled from <code>0</code> to <code>n - 1</code>. The edges in the graph are represented by a given 2D integer array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes an edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself.</p>

<p>Return <em>the length of the <strong>shortest </strong>cycle in the graph</em>. If no cycle exists, return <code>-1</code>.</p>

<p>A cycle is a path that starts and ends at the same node, and each edge in the path is used only once.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/04/cropped.png" style="width: 387px; height: 331px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[2,0],[3,4],[4,5],[5,6],[6,3]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The cycle with the smallest length is : 0 -&gt; 1 -&gt; 2 -&gt; 0 
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/04/croppedagin.png" style="width: 307px; height: 307px;" />
<pre>
<strong>Input:</strong> n = 4, edges = [[0,1],[0,2]]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There are no cycles in this graph.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= n &lt;= 1000</code></li>
	<li><code>1 &lt;= edges.length &lt;= 1000</code></li>
	<li><code>edges[i].length == 2</code></li>
	<li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt; n</code></li>
	<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
	<li>There are no repeated edges.</li>
</ul>

class Solution:
    def findShortestCycle(self, n: int, edges: List[List[int]]) -> int:
      edges_dict=dict() # this gonna be like {1: {3}, 2: {3}, 3: {1, 2}}
      for u,v in edges:
        if u in edges_dict:
          edges_dict[u].add(v)
        else:
          edges_dict[u]={v}
        if v in edges_dict:
          edges_dict[v].add(u)
        else:
          edges_dict[v]={u}
      
      print(edges_dict)

      # initialize queue
      queue=list()
      for v in edges_dict: # all vertexes that exists as start point
        queue.append([v,v,-1]) # vertexes in list means [start, current, previous]

      distance=0
      previous_path = set()
      founded_distance = 0
      while(queue):
        queue_next=list()
        path = set()
        new_previous_path = set()
        reversed_path = set()
        for start,current,previous in queue: # cycle search nodes in same distance(depth)
          for next_v in edges_dict[current]:
            if next_v != previous:
              if (start,next_v) not in path:
                if next_v==start:
                  print("", start, previous, current)
                  raise NameError
                  return distance+1 # cycle found!
                if (start,next_v) in previous_path:
                  print("previous", distance*2+1)
                  if founded_distance:
                    founded_distance = min(founded_distance, distance*2+1)
                  else:
                    founded_distance = distance*2+1
                  print("previous founded_distance", founded_distance)
                  # return distance*2+1
                if (next_v,start) in path:
                  # print("same",start, previous, current, next_v)
                  if founded_distance:
                    founded_distance = min(founded_distance, (distance+1)*2)
                  else:
                    founded_distance = (distance+1)*2
                  # return (distance+1)*2
                queue_next.append((start,next_v,current))
                path.add((next_v,start)) # skips case going reversed path
                new_previous_path.add((start,next_v))
                # reversed_path.add((next_v,start))
        queue=queue_next
        distance+=1
        if founded_distance != 0:
          if distance+1>=founded_distance:
            print("suc")
            return founded_distance
        previous_path = new_previous_path
      return -1 # no cycle        
253