You are viewing a single comment's thread. Return to all comments →
Python
def findShortest(graph_nodes, graph_from, graph_to, ids, val): graph = defaultdict(set) for start, end in zip(graph_from, graph_to): graph[start].add(end) graph[end].add(start) min_path_len = math.inf for node, node_color in enumerate(ids, start=1): if node_color != val: continue visited = set([node]) queue = deque(graph[node]) curr_path_len = 0 found_at = None while queue: curr = queue.popleft() if curr in visited: continue visited.add(curr) curr_path_len += 1 curr_color = ids[curr-1] if curr_color == node_color: found_at = curr_path_len break queue.extend(graph[curr]) if found_at: min_path_len = min(min_path_len, found_at) if min_path_len == math.inf: return -1 return min_path_len
Seems like cookies are disabled on this browser, please enable them to open this website
Find the nearest clone
You are viewing a single comment's thread. Return to all comments →
Python