Пример #1
0
def is_eulerian(graph):
    """ Returns True if the graph is Eulerian. """

    if graph.is_directed():
        if is_connected(graph):
            return all((
                graph.in_degree(node) == graph.out_degree(node)
                for node in graph))
    else:
        if is_connected(graph):
            return all((
                graph.degree(node) % 2 == 0
                for node in graph))
    return False
Пример #2
0
def is_tree(graph):
    ''' Returns whether graph is a tree.
        This implementation only allows
        undirected graphs! '''
    if is_connected(graph):
        if graph.order() == graph.size() + 1:
            return True
    return False
Пример #3
0
def is_bipartite(graph):
    ''' If graph is bipartite, returns two sets
        of nodes U and V, such that every edge
        is from a node in U to node in V. If not,
        returns None. Graph must be connected! '''

    if not is_connected(graph):
        return None
    start = next(graph.__iter__())
    black, red = True, False
    color = {start: black}
    stack = deque([start])
    while stack:
        u = stack.pop()
        for v in graph[u]:
            if v not in color:
                color[v] = not color[u]
                stack.append(v)
            elif color[v] == color[u]:
                return None

    sets = _groupby(lambda x: color[x], color)
    return (sets[black], sets[red])