示例#1
0
def test_graph_add_edges(using_opengl_renderer):
    G = Graph([1, 2, 3, 4, 5], [(1, 2), (2, 3)])
    added_mobjects = G.add_edges((1, 3))
    assert isinstance(added_mobjects.submobjects[0], Line)
    assert len(G.vertices) == 5
    assert len(G.edges) == 3
    assert set(G.vertices.keys()) == {1, 2, 3, 4, 5}
    assert set(G.edges.keys()) == {(1, 2), (2, 3), (1, 3)}

    added_mobjects = G.add_edges((1, 42))
    removed_mobjects = added_mobjects.submobjects
    assert isinstance(removed_mobjects[0], Dot)
    assert isinstance(removed_mobjects[1], Line)

    assert len(G.vertices) == 6
    assert len(G.edges) == 4
    assert set(G.vertices.keys()) == {1, 2, 3, 4, 5, 42}
    assert set(G.edges.keys()) == {(1, 2), (2, 3), (1, 3), (1, 42)}

    added_mobjects = G.add_edges((4, 5), (5, 6), (6, 7))
    assert len(added_mobjects) == 5
    assert len(G.vertices) == 8
    assert len(G.edges) == 7
    assert set(G.vertices.keys()) == {1, 2, 3, 4, 5, 42, 6, 7}
    assert set(G._graph.nodes()) == set(G.vertices.keys())
    assert set(G.edges.keys()) == {
        (1, 2),
        (2, 3),
        (1, 3),
        (1, 42),
        (4, 5),
        (5, 6),
        (6, 7),
    }
    assert set(G._graph.edges()) == set(G.edges.keys())
示例#2
0
def test_graph_add_edges():
    G = Graph([1, 2, 3, 4, 5], [(1, 2), (2, 3)])
    added_mobjects = G.add_edges((1, 3))
    assert str(added_mobjects.submobjects) == "[Line]"
    assert str(G) == "Graph on 5 vertices and 3 edges"
    assert set(G.vertices.keys()) == {1, 2, 3, 4, 5}
    assert set(G.edges.keys()) == {(1, 2), (2, 3), (1, 3)}

    added_mobjects = G.add_edges((1, 42))
    assert str(added_mobjects.submobjects) == "[Dot, Line]"
    assert str(G) == "Graph on 6 vertices and 4 edges"
    assert set(G.vertices.keys()) == {1, 2, 3, 4, 5, 42}
    assert set(G.edges.keys()) == {(1, 2), (2, 3), (1, 3), (1, 42)}

    added_mobjects = G.add_edges((4, 5), (5, 6), (6, 7))
    assert len(added_mobjects) == 5
    assert str(G) == "Graph on 8 vertices and 7 edges"
    assert set(G.vertices.keys()) == {1, 2, 3, 4, 5, 42, 6, 7}
    assert set(G._graph.nodes()) == set(G.vertices.keys())
    assert set(G.edges.keys()) == {
        (1, 2),
        (2, 3),
        (1, 3),
        (1, 42),
        (4, 5),
        (5, 6),
        (6, 7),
    }
    assert set(G._graph.edges()) == set(G.edges.keys())
示例#3
0
def test_graph_creation():
    vertices = [1, 2, 3, 4]
    edges = [(1, 2), (2, 3), (3, 4), (4, 1)]
    layout = {1: [0, 0, 0], 2: [1, 1, 0], 3: [1, -1, 0], 4: [-1, 0, 0]}
    G_manual = Graph(vertices=vertices, edges=edges, layout=layout)
    assert str(G_manual) == "Graph on 4 vertices and 4 edges"
    G_spring = Graph(vertices=vertices, edges=edges)
    assert str(G_spring) == "Graph on 4 vertices and 4 edges"
示例#4
0
def test_graph_creation(using_opengl_renderer):
    vertices = [1, 2, 3, 4]
    edges = [(1, 2), (2, 3), (3, 4), (4, 1)]
    layout = {1: [0, 0, 0], 2: [1, 1, 0], 3: [1, -1, 0], 4: [-1, 0, 0]}
    G_manual = Graph(vertices=vertices, edges=edges, layout=layout)
    assert len(G_manual.vertices) == 4
    assert len(G_manual.edges) == 4
    G_spring = Graph(vertices=vertices, edges=edges)
    assert len(G_spring.vertices) == 4
    assert len(G_spring.edges) == 4
示例#5
0
def test_graph_remove_vertices():
    G = Graph([1, 2, 3, 4, 5], [(1, 2), (2, 3), (3, 4), (4, 5)])
    removed_mobjects = G.remove_vertices(3)
    assert len(removed_mobjects) == 3
    assert str(G) == "Graph on 4 vertices and 2 edges"
    assert list(G.vertices.keys()) == [1, 2, 4, 5]
    assert list(G.edges.keys()) == [(1, 2), (4, 5)]
    removed_mobjects = G.remove_vertices(4, 5)
    assert len(removed_mobjects) == 3
    assert str(G) == "Graph on 2 vertices and 1 edges"
    assert list(G.vertices.keys()) == [1, 2]
    assert list(G.edges.keys()) == [(1, 2)]
示例#6
0
def test_graph_remove_edges():
    G = Graph([1, 2, 3, 4, 5], [(1, 2), (2, 3), (3, 4), (4, 5), (1, 5)])
    removed_mobjects = G.remove_edges((1, 2))
    assert str(removed_mobjects.submobjects) == "[Line]"
    assert str(G) == "Graph on 5 vertices and 4 edges"
    assert set(G.edges.keys()) == {(2, 3), (3, 4), (4, 5), (1, 5)}
    assert set(G._graph.edges()) == set(G.edges.keys())

    removed_mobjects = G.remove_edges((2, 3), (3, 4), (4, 5), (5, 1))
    assert len(removed_mobjects) == 4
    assert str(G) == "Graph on 5 vertices and 0 edges"
    assert set(G._graph.edges()) == set()
    assert set(G.edges.keys()) == set()
示例#7
0
def test_graph_remove_vertices(using_opengl_renderer):
    G = Graph([1, 2, 3, 4, 5], [(1, 2), (2, 3), (3, 4), (4, 5)])
    removed_mobjects = G.remove_vertices(3)
    assert len(removed_mobjects) == 3
    assert len(G.vertices) == 4
    assert len(G.edges) == 2
    assert list(G.vertices.keys()) == [1, 2, 4, 5]
    assert list(G.edges.keys()) == [(1, 2), (4, 5)]
    removed_mobjects = G.remove_vertices(4, 5)
    assert len(removed_mobjects) == 3
    assert len(G.vertices) == 2
    assert len(G.edges) == 1
    assert list(G.vertices.keys()) == [1, 2]
    assert list(G.edges.keys()) == [(1, 2)]
示例#8
0
def test_graph_remove_edges(using_opengl_renderer):
    G = Graph([1, 2, 3, 4, 5], [(1, 2), (2, 3), (3, 4), (4, 5), (1, 5)])
    removed_mobjects = G.remove_edges((1, 2))
    assert isinstance(removed_mobjects.submobjects[0], Line)
    assert len(G.vertices) == 5
    assert len(G.edges) == 4
    assert set(G.edges.keys()) == {(2, 3), (3, 4), (4, 5), (1, 5)}
    assert set(G._graph.edges()) == set(G.edges.keys())

    removed_mobjects = G.remove_edges((2, 3), (3, 4), (4, 5), (5, 1))
    assert len(removed_mobjects) == 4
    assert len(G.vertices) == 5
    assert len(G.edges) == 0
    assert set(G._graph.edges()) == set()
    assert set(G.edges.keys()) == set()
示例#9
0
def test_graph_add_vertices():
    G = Graph([1, 2, 3], [(1, 2), (2, 3)])
    G.add_vertices(4)
    assert str(G) == "Graph on 4 vertices and 2 edges"
    G.add_vertices(5, labels={5: Text("5")})
    assert str(G) == "Graph on 5 vertices and 2 edges"
    assert 5 in G._labels
    assert 5 in G._vertex_config
    G.add_vertices(6, 7, 8)
    assert len(G.vertices) == 8
    assert len(G._graph.nodes()) == 8
示例#10
0
def test_graph_add_vertices(using_opengl_renderer):
    G = Graph([1, 2, 3], [(1, 2), (2, 3)])
    G.add_vertices(4)
    assert len(G.vertices) == 4
    assert len(G.edges) == 2
    G.add_vertices(5, labels={5: Text("5")})
    assert len(G.vertices) == 5
    assert len(G.edges) == 2
    assert 5 in G._labels
    assert 5 in G._vertex_config
    G.add_vertices(6, 7, 8)
    assert len(G.vertices) == 8
    assert len(G._graph.nodes()) == 8
示例#11
0
def test_custom_animation_mobject_list():
    G = Graph([1, 2, 3], [(1, 2), (2, 3)])
    scene = Scene()
    scene.add(G)
    assert scene.mobjects == [G]
    with tempconfig({"dry_run": True, "quality": "low_quality"}):
        scene.play(G.animate.add_vertices(4))
        assert str(G) == "Graph on 4 vertices and 2 edges"
        assert scene.mobjects == [G]
        scene.play(G.animate.remove_vertices(2))
        assert str(G) == "Graph on 3 vertices and 0 edges"
        assert scene.mobjects == [G]
示例#12
0
 def construct(self):
     G = Graph([], [], labels=True)
     G.scale(0.45)
     G.add_vertices(1, positions={1: [0, 0, 0]}, labels=True)
     self.play(Create(G))
     self.wait(10)