-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.py
More file actions
34 lines (29 loc) · 1.02 KB
/
Copy pathsolution.py
File metadata and controls
34 lines (29 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from collections import deque
class Solution:
def shortCycle(self, V, edges):
# build adjacency list
adj = [[] for _ in range(V)]
for u, v in edges:
adj[u].append(v)
adj[v].append(u)
ans = float('inf')
# BFS from every vertex
for s in range(V):
dist = [-1] * V
parent = [-1] * V
dq = deque()
dist[s] = 0
dq.append(s)
while dq:
u = dq.popleft()
for v in adj[u]:
if dist[v] == -1:
dist[v] = dist[u] + 1
parent[v] = u
dq.append(v)
elif parent[u] != v:
# visited and not parent => cycle found
cycle_len = dist[u] + dist[v] + 1
if cycle_len < ans:
ans = cycle_len
return -1 if ans == float('inf') else ans