<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>

"""
Scratch:
Run a BFS
When processing nbrs of u:
    Suppose v already marked as visited in this round
    Need to identify whether u was added by v

preds: preds[u] tracks who added u to queue
"""
LARGE_VAL = 10 ** 9 + 7
class Solution:
    def findShortestCycle(self, n: int, edges: List[List[int]]) -> int:
        self.nbrs = [[] for _ in range(n)]        
        self.build_nbrs(edges)
        self.n = n
        self.best = [LARGE_VAL for _ in range(n)]
        for i in range(n):
            self.run_bfs(i)
        res = min(self.best)
        return res if res != LARGE_VAL else -1

    def build_nbrs(self, edges):
        for u, v in edges:
            #print(f"Adding edge from {u} to {v}")
            self.nbrs[u].append(v)
            self.nbrs[v].append(u)
    
    def run_bfs(self, i):
        #print(f"Running bfs from {i}")
        # distance from i to node
        self.dists = [-1 for _ in range(self.n)]
        q = deque()
        q.append(i)
        self.dists[i] = 0
        self.preds = [-1 for _ in range(self.n)]
        while q:
            curr_node = q.popleft()
            #print(f"Processing node {curr_node}")
            curr_dist = self.dists[curr_node]
            curr_nbrs = self.nbrs[curr_node]
            for nb in curr_nbrs:
                #print(f"Processing neighbor {nb}")
                #print(f"Curr dist is: {curr_dist}")
                if self.dists[nb] == -1:  # unvisited
                    #print(f"Unvisited, putting in q with dist {curr_dist + 1}")
                    self.dists[nb] = curr_dist + 1
                    self.preds[nb] = curr_node
                    q.append(nb)
                else:  # visited before
                    if self.preds[curr_node] != nb:  # no direct back-edges
                        cycle_dist = curr_dist + self.dists[nb] + 1
                        self.best[i] = min(self.best[i], cycle_dist)
                        #print(f"Found cycle with length {cycle_dist}")



        
1855