def IsValid(G,degree):
    """
    Method checks if a graph is valid, i.e. the real
    degree of the vertices matches the character
    in the lgi-string.
    G      -- Graph.
    degree -- Expected degree
    Return:
    True if all degrees match the character in the string.
    """
    for idx in range(G.number_of_nodes()):
        expt = degree[idx]
        act = G.degree(idx)
        if act != expt:
            return False
    return True