Exemplo n.º 1
0
def read_graph_from_file(filename):
    """
    Read in data from the specified filename, and create and return a graph
    object corresponding to that data.

    Arguments:
    filename (string): The relative path of the file to be processed

    Returns:
    Graph: A directed or undirected Graph object containing the specified
    vertices and edges
    """

    # TODO: Use 'open' to open the file
    with open(filename) as f:
        lines = next(f).strip('\n')

        if lines == "G":
            graph = Graph(is_directed=False)
        elif lines == "D":
            graph = Graph()
        else:
            raise ValueError('Invalid graph type')

        next_line = next(f).strip('\n').split(',')

        for _ in next_line:
            graph.add_vertex(_)

        for line in f:
            graph.add_edge(line[1], line[3])

        return graph
Exemplo n.º 2
0
def read_graph_from_file(filename):
    """
    Read in data from the specified filename, and create and return a graph
    object corresponding to that data.

    Arguments:
    filename (string): The relative path of the file to be processed

    Returns:
    Graph: A directed or undirected Graph object containing the specified
    vertices and edges
    """

    with open(filename) as file:
        file_it = iter(file)
        graph_type = {'D': True, 'G': False}.get(next(file_it).strip('\n'))

        if graph_type is None:
            raise ValueError()
        graph = Graph(is_directed=graph_type)

        for num in next_alnum(next(file_it)):
            ## Use the second line to add the vertices to the graph
            graph.add_vertex(num)

        for line in file_it:
            ## Use the 3rd+ line to add the edges to the graph
            lineit = next_alnum(line)

            try:
                node1, node2 = next(lineit), next(lineit)
                graph.add_edge(node1, node2)
            except:
                pass
    return graph
def read_graph_from_file(filename):
    """
    Read in data from the specified filename, and create and return a graph
    object corresponding to that data.

    Arguments:
    filename (string): The relative path of the file to be processed

    Returns:
    Graph: A directed or undirected Graph object containing the specified
    vertices and edges
    """

    # Use 'open' to open the file
    with open(filename) as graph_file:
        graph_file_lines = graph_file.readlines()

    # Use the first line (G or D) to determine whether graph is directed
    # and create a graph object
    direction = graph_file_lines[0].strip()
    if direction != 'G' and direction != 'D':
        raise ValueError('File is in an imporper format')
    graph = Graph(is_directed=direction is 'D')

    # Use the second line to add the vertices to the graph
    for vertex in graph_file_lines[1].strip().split(','):
        graph.add_vertex(vertex)

    # Use the 3rd+ line to add the edges to the graph
    for edge in graph_file_lines[2:]:
        edge = edge.strip().split(',')
        graph.add_edge(edge[0][1], edge[1][0])

    graph_file.close()
    return graph
Exemplo n.º 4
0
def read_graph_from_file(filename):
    """
    Read in data from the specified filename, and create and return a graph
    object corresponding to that data.

    Arguments:
    filename (string): The relative path of the file to be processed

    Returns:
    Graph: A directed or undirected Graph object containing the specified
    vertices and edges
    """

    # TODO: Use 'open' to open the file
    with open(filename, 'r', encoding='utf-8-sig') as f:
        # TODO: Use the first line (G or D) to determine whether graph is directed
        first = next(f).strip('\n')

        if first == 'D':
            graph = Graph(is_directed=True)
        elif first == 'G':
            graph = Graph(is_directed=False)
        else:
            raise ValueError('Invalid file format')

        # TODO: Use the second line to add the vertices to the graph
        for each in next(f).strip('\n').split(','):
            graph.add_vertex(each)

        # TODO: Use the 3rd+ line to add the edges to the graph
        for line in f:
            graph.add_edge(line[1], line[3])

        return graph
    def test_connected_components_directed(self):
        """Create a graph."""
        graph = Graph(is_directed=True)
        graph.add_vertex('A')
        graph.add_vertex('B')
        graph.add_vertex('C')

        graph.add_vertex('D')
        graph.add_vertex('E')
        graph.add_vertex('F')

        graph.add_vertex('G')
        graph.add_vertex('H')

        graph.add_edge('A', 'B')
        graph.add_edge('B', 'C')
        graph.add_edge('C', 'A')

        graph.add_edge('D', 'E')
        graph.add_edge('E', 'F')

        graph.add_edge('G', 'H')
        graph.add_edge('G', 'F')

        connected_components = sorted([
            sorted(component)
            for component in graph.get_connected_components()
        ])
        answer = sorted([
            sorted(['A', 'B', 'C']),
            sorted(['D', 'E', 'F', 'G', 'H']),
        ])
        self.assertListEqual(connected_components, answer)
    def test_has_cycle(self):
        """Create a graph."""
        graph = Graph(is_directed=True)
        graph.add_vertex('A')
        graph.add_vertex('B')
        graph.add_vertex('C')

        graph.add_vertex('D')
        graph.add_vertex('E')
        graph.add_vertex('F')

        graph.add_vertex('G')
        graph.add_vertex('H')

        graph.add_edge('A', 'B')
        graph.add_edge('B', 'C')
        graph.add_edge('C', 'A')

        graph.add_edge('D', 'E')
        graph.add_edge('E', 'F')

        graph.add_edge('G', 'H')
        graph.add_edge('G', 'F')

        self.assertEqual(True, graph.contains_cycle())
Exemplo n.º 7
0
    def test_get_edges(self):
        graph = Graph()

        # Create test Verticies
        v1,v2,v3 = Vertex("a"), Vertex("b"), Vertex("c")

        # Add verticies
        graph.add_vertex(v1)
        graph.add_vertex(v2)
        graph.add_vertex(v3)

        self.assertEqual(graph.num_verticies, 3)
        self.assertEqual(graph.num_edges, 0)

        # Create edges
        edges = [
            ("a", "b", 10),
            ("b", "c", 10),
            ("c", "a", 4)
        ]

        # Iterate through edges
        for edge in edges:
            fromVert, toVert, weight = edge
            graph.add_edge(fromVert, toVert, weight)

        self.assertEqual(graph.num_edges, 3)
    def test_is_not_bipartite(self):
        """Create a graph."""
        graph = Graph(is_directed=False)
        graph.add_vertex('A')
        graph.add_vertex('B')
        graph.add_vertex('C')
        graph.add_vertex('D')
        graph.add_vertex('E')
        graph.add_edge('A', 'B')
        graph.add_edge('B', 'D')
        graph.add_edge('D', 'C')
        graph.add_edge('C', 'A')
        # These two cause the rule to break
        graph.add_edge('C', 'E')
        graph.add_edge('D', 'E')
        self.assertEqual(graph.is_bipartite(), False)

        graph = Graph(is_directed=True)
        graph.add_vertex('A')
        graph.add_vertex('B')
        graph.add_vertex('C')
        graph.add_vertex('D')
        graph.add_edge('A', 'B')
        graph.add_edge('A', 'C')
        graph.add_edge('A', 'D')
        # This one causes the rule to break
        graph.add_edge('B', 'D')
        self.assertEqual(graph.is_bipartite(), False)
Exemplo n.º 9
0
def read_graph_from_file(filename):
    """
    Read in data from the specified filename, and create and return a graph
    object corresponding to that data.

    Arguments:
    filename (string): The relative path of the file to be processed

    Returns:
    Graph: A directed or undirected Graph object containing the specified
    vertices and edges
    """
    # Use 'open' to open the file
    # Use the first line (G or D) to determine whether graph is directed
    # and create a graph object
    f = open(filename).read().split()

    if f[0] not in ["D", "G"]:
        raise ValueError("File not proper format")
    directed = True if f[0] == "D" else False
    graph = Graph(directed)

    # Use the second line to add the vertices to the graph
    verticies = f[1].split(',')

    for vertex in verticies:
        graph.add_vertex(vertex)

    # Use the 3rd+ line to add the edges to the graph
    for edge in f[2:]:
        v1, v2 = edge[1:len(edge) - 1].split(',')
        graph.add_edge(v1, v2)

    return graph
Exemplo n.º 10
0
def read_graph_from_file(filename):
    """
    Read in data from the specified filename, and create and return a graph
    object corresponding to that data.

    Arguments:
    filename (string): The relative path of the file to be processed

    Returns:
    Graph: A directed or undirected Graph object containing the specified
    vertices and edges
    """
    my_file = open(filename)

    graph_type = my_file.readline().strip()
    if graph_type == "G":
        graph = Graph(False)
    elif graph_type == "D":
        graph = Graph(True)
    else:
        raise ValueError("Unexpected character")

    vertices = my_file.readline().strip().split(",")
    for vertex in vertices:
        graph.add_vertex(vertex)

    for edge in my_file:
        vertex1, vertex2 = edge.strip()[1:-1].split(",")
        graph.add_edge(vertex1, vertex2)

    return graph

    pass
Exemplo n.º 11
0
def read_graph_from_file(filename):
    """
    Read in data from the specified filename, and create and return a graph
    object corresponding to that data.

    Arguments:
    filename (string): The relative path of the file to be processed

    Returns:
    Graph: A directed or undirected Graph object containing the specified
    vertices and edges
    """

    # Use 'open' to open the file
    f = open(filename, "r").read().split()

    # Use the first line (G or D) to determine whether graph is directed
    # and create a graph object
    is_directed = True if f[0] == "D" else False
    graph = Graph(is_directed=is_directed)

    # Use the second line to add the vertices to the graph
    vertices = f[1].split(',')
    for v in vertices:
        graph.add_vertex(v)

    # Use the 3rd+ line to add the edges to the graph
    edges = f[2:]
    for e in edges:
        v1, v2 = e.strip(')(').split(',')
        graph.add_edge(v1, v2)

    return graph
Exemplo n.º 12
0
def read_graph_from_file(filename):
    """
    Read in data from the specified filename, and create and return a graph
    object corresponding to that data.

    Arguments:
    filename (string): The relative path of the file to be processed

    Returns:
    Graph: A directed or undirected Graph object containing the specified
    vertices and edges
    """

    # Use 'open' to open the file
    f = open(filename, "r")

    # Use the first line (G or D) to determine whether graph is directed
    # and create a graph object
    first_line = f.readline().strip()
    graph = Graph(False)

    # If undirected
    if first_line == "G":
        graph = Graph(False)

    # If directed
    elif first_line == "D":
        graph = Graph()

    else:
        print("Invalid Input")
        print(first_line)

    # Use the second line to add the vertices to the graph
    vertices = f.readline().strip()
    for _ in vertices:
        graph.add_vertex(_)

    # Use the 3rd+ line to add the edges to the graph
    for line in f:
        if line != '':
            print(line)
            curr = line.replace('(', '')
            curr = curr.replace(')', '').strip()
            curr = curr.split(",")
            print("Current line: {}".format(curr))

            if curr:
                vert1 = graph.add_vertex(curr[0])
                vert2 = graph.add_vertex(curr[1])
                # print("Vert 1: {} Vert 2: {}".format(vert1, vert2))

                graph.add_edge(vert1.get_id(), vert2.get_id())

    f.close()
    return graph
Exemplo n.º 13
0
    def test_does_contain_cycle(self):
        graph = Graph(is_directed=True)
        graph.add_vertex('A')
        graph.add_vertex('B')
        graph.add_vertex('C')
        graph.add_edge('A', 'B')
        graph.add_edge('B', 'C')
        graph.add_edge('C', 'A')

        self.assertTrue(graph.contains_cycle())
def courseOrder(numCourses, prerequisites):
    """Return a course schedule according to the prerequisites provided."""
    graph = Graph(is_directed=True)

    for i in range(numCourses):
        graph.add_vertex(i)

    for elm in prerequisites:
        graph.add_edge(elm[1], elm[0])

    return graph.topological_sort()
Exemplo n.º 15
0
    def test_contains_cycle_undirected(self):
        graph = Graph(is_directed=False)
        graph.add_vertex('A')
        graph.add_vertex('B')
        graph.add_vertex('C')
        graph.add_edge('A','B')
        graph.add_edge('B','C')
        graph.add_edge('C','A')

        # This would be true if graph were directed
        self.assertTrue(graph.contains_cycle())
Exemplo n.º 16
0
class GraphTest(unittest.TestCase):
    V1_LABEL = 1
    V2_LABEL = 2
    V3_LABEL = 3
    V4_LABEL = 4
    V5_LABEL = 5

    E1_LABEL = 1
    E2_LABEL = 2
    E3_LABEL = 3

    if settings.INTEGER_VERTICES:
        v1 = 1
        v2 = 2
        v3 = 3
        v4 = 4
        v5 = 5
    else:
        v1 = Vertex(V1_LABEL)
        v2 = Vertex(V2_LABEL)
        v3 = Vertex(V3_LABEL)
        v4 = Vertex(V4_LABEL)
        v5 = Vertex(V5_LABEL)

    e1 = Edge(E1_LABEL, v1, v2, 3)
    e2 = Edge(E2_LABEL, v2, v1, 5)
    e3 = Edge(E3_LABEL, v4, v5, 10)

    def setUp(self):
        self.g = Graph()

    def test_get_vertex(self):
        self.g.add_vertex(self.v1)
        self.assertEqual(self.g.get_vertex(self.V1_LABEL), self.v1)

    def test_get_edge_ends(self):
        self.assertTupleEqual(self.g.get_edge_ends(self.e1), (self.v1, self.v2))

    def test_get_vertices_count(self):
        self.assertEqual(self.g.get_vertices_count(), 0)
        self.g.add_vertex(self.v1)
        self.assertEqual(self.g.get_vertices_count(), 1)
        self.g.add_vertex(self.v2)
        self.assertEqual(self.g.get_vertices_count(), 2)

    def test_get_vertex_position(self):
        if settings.INTEGER_VERTICES:
            return
        self.g.add_vertex(self.v1)
        self.g.add_vertex(self.v2)
        self.g.add_vertex(self.v3)
        self.assertEqual(self.g.get_vertex_position(self.v1), 0)
        self.assertEqual(self.g.get_vertex_position(self.v2), 1)
        self.assertEqual(self.g.get_vertex_position(self.v3), 2)
Exemplo n.º 17
0
    def test_does_not_contain_cycle_dag(self):
        """Test that a DAG does not contain a cycle."""
        graph = Graph(is_directed=True)
        graph.add_vertex('A')
        graph.add_vertex('B')
        graph.add_vertex('C')
        graph.add_edge('A', 'B')
        graph.add_edge('B', 'C')
        graph.add_edge('A', 'C')

        self.assertFalse(graph.contains_cycle())
Exemplo n.º 18
0
    def test_find_path_dfs(self):
        graph = Graph(is_directed=True)
        graph.add_vertex('A')
        graph.add_vertex('B')
        graph.add_vertex('C')
        graph.add_edge('A', 'B')
        graph.add_edge('B', 'C')
        graph.add_edge('C', 'A')

        path = graph.find_path_dfs_iter('A', 'C')
        self.assertEqual(path, ['A', 'B', 'C'])
Exemplo n.º 19
0
    def test_not_bipartite(self):
        """Test that a cycle on 3 vertices is NOT bipartite."""
        graph = Graph(is_directed=False)
        graph.add_vertex('A')
        graph.add_vertex('B')
        graph.add_vertex('C')
        graph.add_edge('A', 'B')
        graph.add_edge('A', 'C')
        graph.add_edge('B', 'C')

        self.assertFalse(graph.is_bipartite())
Exemplo n.º 20
0
    def test_does_not_contain_cycle_tree(self):
        """Test that a tree on 4 vertices does not contain a cycle."""
        graph = Graph(is_directed=True)
        vertex_a = graph.add_vertex('A')
        vertex_b = graph.add_vertex('B')
        vertex_c = graph.add_vertex('C')
        vertex_d = graph.add_vertex('D')
        graph.add_edge('A', 'B')
        graph.add_edge('A', 'C')
        graph.add_edge('A', 'D')

        self.assertFalse(graph.contains_cycle())
Exemplo n.º 21
0
    def text_get_verticies(self):
        graph = Graph()

        # Create test Verticies
        v1,v2,v3 = Vertex("a"), Vertex("b"), Vertex("c")

        # Add verticies
        graph.add_vertex(v1)
        graph.add_vertex(v2)
        graph.add_vertex(v3)

        self.assertListEqual(graph.get_vertices, ["a","b","c"])
Exemplo n.º 22
0
    def test_is_bipartite_tree(self):
        """Test that a tree on 4 vertices is bipartite."""
        graph = Graph(is_directed=False)
        vertex_a = graph.add_vertex('A')
        vertex_b = graph.add_vertex('B')
        vertex_c = graph.add_vertex('C')
        vertex_d = graph.add_vertex('D')
        graph.add_edge('A', 'B')
        graph.add_edge('A', 'C')
        graph.add_edge('A', 'D')

        self.assertTrue(graph.is_bipartite())
Exemplo n.º 23
0
    def test_is_bipartite_cycle(self):
        """Test that a cycle on 4 vertices is bipartite."""
        graph = Graph(is_directed=False)
        graph.add_vertex('A')
        graph.add_vertex('B')
        graph.add_vertex('C')
        graph.add_vertex('D')
        graph.add_edge('A', 'B')
        graph.add_edge('B', 'C')
        graph.add_edge('C', 'D')
        graph.add_edge('A', 'D')

        self.assertTrue(graph.is_bipartite())
Exemplo n.º 24
0
 def to_graph(self):
     """
     Create a graph from this tree, vertices pointing toward their parents.
     """
     g = Graph()
     count = 0
     for vertex in self.preorder():
         vertex._index = count
         g.add_vertex(vertex._index, vertex.value)
         if vertex.parent is not None:
             g.add_edge(vertex._index, vertex._index, vertex.parent._index)
         count += 1
     return g
Exemplo n.º 25
0
def read_graph_from_file(filename):
    """
    Read in data from the specified filename, and create and return a graph
    object corresponding to that data.

    Arguments:
    filename (string): The relative path of the file to be processed

    Returns:
    Graph: A directed or undirected Graph object containing the specified
    vertices and edges
    """

    # TODO: Use 'open' to open the file

    # TODO: Use the first line (G or D) to determine whether graph is directed
    # and create a graph object

    # TODO: Use the second line to add the vertices to the graph

    # TODO: Use the 3rd+ line to add the edges to the graph

    graph_obj = None
    with open(filename) as graph_file:
        for index, line in enumerate(graph_file.readlines()):
            line = line.strip()
            if index == 0:
                line_parts = line.split(", ")
                if line_parts[0] not in ("D", "G"):
                    raise (ValueError("Bad graph type"))
                    return
                graph_obj = Graph(is_directed=(line_parts[0] == "D"),
                                  lat=float(line_parts[1]),
                                  lng=float(line_parts[2]))
            elif index == 1:
                for vertex in line.split(';'):
                    vertex_id, sweetness, saltiness, savoriness, num_stars = vertex.split(
                        ", ")
                    if vertex_id == "":
                        continue
                    else:
                        graph_obj.add_vertex(vertex_id, sweetness, saltiness,
                                             savoriness, num_stars)
            else:
                if line == "":
                    continue
                vertices = line[1:-1].split(',')
                graph_obj.add_edge(vertices[0], vertices[1])
    return graph_obj
    def test_create_undirected_graph(self):
        """Create a graph."""
        graph = Graph(is_directed=False)
        graph.add_vertex('A')
        graph.add_vertex('B')
        graph.add_vertex('C')
        graph.add_edge('A', 'B')
        graph.add_edge('A', 'C')
        graph.add_edge('B', 'C')

        self.assertEqual(len(graph.get_vertices()), 3)

        self.assertEqual(len(graph.get_neighbors('A')), 2)
        self.assertEqual(len(graph.get_neighbors('B')), 2)
        self.assertEqual(len(graph.get_neighbors('C')), 2)
Exemplo n.º 27
0
    def test_create_directed_graph(self):
        """Create a graph."""
        graph = Graph(is_directed=True)
        vertex_a = graph.add_vertex('A')
        vertex_b = graph.add_vertex('B')
        vertex_c = graph.add_vertex('C')
        graph.add_edge('A', 'B')
        graph.add_edge('A', 'C')
        graph.add_edge('B', 'C')

        self.assertEqual(len(graph.get_vertices()), 3)

        self.assertEqual(len(vertex_a.get_neighbors()), 2)
        self.assertEqual(len(vertex_b.get_neighbors()), 1)
        self.assertEqual(len(vertex_c.get_neighbors()), 0)
Exemplo n.º 28
0
 def to_graph(self):
     """
     Create a graph from this tree, vertices pointing toward their parents.
     """
     g = Graph()
     count = 0
     for vertex in self.preorder():
         vertex._index = count
         g.add_vertex(vertex._index, vertex.value)
         if vertex.parent is not None:
             g.add_edge(vertex._index, vertex._index, vertex.parent._index)
         count += 1
     return g
         
         
         
Exemplo n.º 29
0
    def test_topological_sort(self):
        graph = Graph(is_directed=True)
        vertex_b = graph.add_vertex('B')
        vertex_c = graph.add_vertex('C')
        vertex_d = graph.add_vertex('D')
        vertex_d = graph.add_vertex('E')
        vertex_a = graph.add_vertex('A')
        graph.add_edge('A', 'C')
        graph.add_edge('B', 'D')
        graph.add_edge('C', 'D')
        graph.add_edge('D', 'E')
        graph.add_edge('A', 'B')

        possible_sorts = [['A', 'B', 'C', 'D', 'E'], ['A', 'C', 'B', 'D', 'E']]
        topo_sort = graph.topological_sort()

        self.assertIn(topo_sort, possible_sorts)
Exemplo n.º 30
0
    def make_graph(self):
        graph = Graph(is_directed=False)
        vertex_a = graph.add_vertex('A')
        vertex_b = graph.add_vertex('B')
        vertex_c = graph.add_vertex('C')
        vertex_c = graph.add_vertex('D')
        vertex_c = graph.add_vertex('E')
        vertex_c = graph.add_vertex('F')

        graph.add_edge('A','B', 4)
        graph.add_edge('A','C', 8)
        graph.add_edge('B','C', 11)
        graph.add_edge('B','D', 8)
        graph.add_edge('C','F', 1)
        graph.add_edge('C','E', 4)
        graph.add_edge('D','E', 2)
        graph.add_edge('E','F', 6)

        return graph
Exemplo n.º 31
0
def read_graph_from_file(filename):
    """
    Read in data from the specified filename, and create and return a graph
    object corresponding to that data.

    Arguments:
    filename (string): The relative path of the file to be processed

    Returns:
    Graph: A directed or undirected Graph object containing the specified
    vertices and edges
    """
    file = open(filename, "r")
    lines = file.readlines()
    file.close()

    # Check if it is a graph or digraph
    graph_or_digraph_str = lines[0].strip() if len(lines) > 0 else None
    if graph_or_digraph_str != "G" and graph_or_digraph_str != "D":
        raise ValueError("File must start with G or D.")
    is_bidirectional = graph_or_digraph_str == "G"

    g = Graph()

    # Add all vertices
    for vertex_key in lines[1].strip("() \n").split(","):
        g.add_vertex(vertex_key)

    # Add all edges
    for line in lines[2:]:
        # Split components of edge
        new_edge = line.strip("() \n").split(",")
        if len(new_edge) < 2 or len(new_edge) > 3:
            raise Exception("Lines adding edges must include 2 or 3 values")

        # Get vertices
        vertex1, vertex2 = new_edge[:2]

        # Add edge(s)
        g.add_edge(vertex1, vertex2)
        if is_bidirectional:
            g.add_edge(vertex2, vertex1)
    return g
Exemplo n.º 32
0
def read_graph_from_file(filename):
    """
    Read in data from the specified filename, and create and return a graph
    object corresponding to that data.

    Arguments:
    filename (string): The relative path of the file to be processed

    Returns:
    Graph: A directed or undirected Graph object containing the specified
    vertices and edges
    """

    # TODO: Use 'open' to open the file

    with open(filename) as f:
        text_lines = f.readlines()

    # TODO: Use the first line (G or D) to determine whether graph is directed
    # and create a graph object

    if text_lines[0].strip() == 'G':
        is_digraph = False
    elif text_lines[0].strip() == 'D':
        is_digraph = True
    else:
        raise ValueError("improper graph type")

    graph = Graph(is_digraph)

    # TODO: Use the second line to add the vertices to the graph

    for vertex_id in text_lines[1].strip('\n').split(','):
        graph.add_vertex(vertex_id)

    # TODO: Use the 3rd+ line to add the edges to the graph

    for edges in text_lines[2:]:
        new_edge = edges.strip('() \n').split(',')
        graph.add_edge(new_edge[0], new_edge[1])

    return graph
Exemplo n.º 33
0
        for (t,vt) in vertices:
            if graph.is_adjacent(s,t):
                assert((s,t) in a)
                aa[(s,t)] = set()
                for e in graph.iter_connections(s,t):
                    aa[(s,t)].add(e)
            if not graph.is_adjacent(s,t):
                assert((s,t) not in a)
    assert(aa==a)
    

                
g = Graph()
assert(str(g) == "<Graph with %d vertices and %d edges>" %(0, 0))
check_graph_validity(g)
g.add_vertex('spam')
check_graph_validity(g)
g.add_edge('E1,2', 1, 2)
check_graph_validity(g)
assert(str(g) == "<Graph with %d vertices and %d edges>" %(3, 1))
check_graph_validity(g)
g.remove_vertex(1)
check_graph_validity(g)
g.clear()
check_graph_validity(g)


g.add_vertex("spam")
g.add_vertex("eggs")

for i in range(0,4):