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

from collections import deque

class Solution:

    # use a bfs approach (start on all possible nodes)
    def findShortestCycle(self, n, edges):
        g = defaultdict(list)
        for u,v in edges:
            g[u].append(v)
            g[v].append(u)
        
        ans = float("inf")
        for i in range(n):
            # do bfs from node i
            # need to try from every node i
            # instead of skipping over nodes previously visited
            # because bfs might have started from afar so
            # overlapping (smaller) cycles were not fully discovered before bfs ended
            visit = {}
            q = deque([(i, -1)]) # current node, parent (need for undirected graph)
            d = 0
            cycle_found = False
            while q:
                if d >= ans:
                    # break early if cycle is going to be longer than ans
                    break
                for j in range(len(q)):
                    n, parent = q.popleft()
                    for nxt in g[n]:
                        if nxt != parent:
                            if nxt in visit:
                                ans = min(ans, visit[nxt] + d + 1)
                                cycle_found = True
                                break
                            visit[nxt] = d + 1
                            q.append((nxt, n))

                d += 1
        if ans == float("inf"):
            return -1
        return ans



    # approach below doesn't work because graph can have overlapping cycles
    # dfs approach, once it detects a cycle (say a big one), won't visit overlapping nodes
    # so the smaller overlapping cycle will never be detected
    def findShortestCycle1(self, n: int, edges: List[List[int]]) -> int:
        # need to detect cycles, which requires us to track visit state
        #   while doing dfs
        # but also need a global visit set to keep track of dead-ends

        g = defaultdict(list)
        for u,v in edges:
            g[u].append(v)
            g[v].append(u)

        visit = set()

        ans = float("inf")
        # do a dfs to find cycles
        def dfs(n, cycle, step, parent):
            if n in visit:
                return float("inf")
                j
            if n in cycle:
                return step - cycle[n]
            cycle[n] = step
            
            for nxt in g[n]:
                if nxt != parent:
                    return dfs(nxt, cycle, step + 1, n)
            
            # no cycle, dead-end
            return float("inf")

                    
        
        for i in range(n):
            if i not in visit:
                cycle = {}
                ret = dfs(i, cycle, 0, -1)
                if ret > 0:
                    ans = min(ans, ret)
                visit.update(cycle.keys())
        
        if ans == float("inf"):
            return -1
        return ans

        
        



        
529