Ejemplo n.º 1
0
def get_sink(G: WeightedDirectedGraph) -> int:
    """
    Return index of output vertex in graph G
    Example:
    >>> get_sink(WeightedDirectedGraph.fromfile(open('wiki_flow.txt')))
    5
    >>> get_sink(WeightedDirectedGraph(0,0,[]))
    Traceback (most recent call last):
        ...
    algorithm.no_sink
    >>> get_sink(WeightedDirectedGraph(3,2,
    ... [WeightedDirectedEdge(0, 1, 1), WeightedDirectedEdge(0, 2, 1)]))
    Traceback (most recent call last):
        ...
    algorithm.ambigous_sink
    """
    V = [v for v in range(G.V()) if len(G.adj(v)) == 0]
    if not V:
        raise no_sink
    if len(V) > 1:
        raise ambigous_sink
    return V[0]