def top_sort(adj_list): n = len(adj_list) colors = ['w'] * n stack = Stack() for i0 in range(n): is_cycle = dfs_colored_step(i0, adj_list, colors, stack) if is_cycle: raise ValueError('Cycle was found') ii_order = stack.to_list() return ii_order
def find_linked_components(adj_list): # only for nonor graphs n = len(adj_list) colors = ['w'] * n components = [] for i0 in range(n): stack = Stack() dfs_colored_step(i0, adj_list, colors, stack) component = set(stack.to_list()) if component: components.append(component) return components