예제 #1
0
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
예제 #2
0
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