Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions manim/mobject/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,12 @@ def _tree_layout(
vertex_spacing: tuple | None = None,
orientation: str = "down",
):
children = {root_vertex: list(T.neighbors(root_vertex))}

if not nx.is_tree(T):
raise ValueError("The tree layout must be used with trees")
if root_vertex is None:
raise ValueError("The tree layout requires the root_vertex parameter")
if not nx.is_tree(T):
raise ValueError("The tree layout must be used with trees")

children = {root_vertex: list(T.neighbors(root_vertex))}
# The following code is SageMath's tree layout implementation, taken from
# https://github.com/sagemath/sage/blob/cc60cfebc4576fed8b01f0fc487271bdee3cefed/src/sage/graphs/graph_plot.py#L1447

Expand Down
14 changes: 14 additions & 0 deletions tests/module/mobject/test_graph.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import pytest

from manim import DiGraph, Graph, Scene, Text, tempconfig


Expand Down Expand Up @@ -100,3 +102,15 @@ def test_custom_animation_mobject_list():
scene.play(G.animate.remove_vertices(2))
assert str(G) == "Undirected graph on 3 vertices and 0 edges"
assert scene.mobjects == [G]


def test_tree_layout_no_root_error():
with pytest.raises(ValueError) as excinfo:
G = Graph([1, 2, 3], [(1, 2), (2, 3)], layout="tree")
assert str(excinfo.value) == "The tree layout requires the root_vertex parameter"


def test_tree_layout_not_tree_error():
with pytest.raises(ValueError) as excinfo:
G = Graph([1, 2, 3], [(1, 2), (2, 3), (3, 1)], layout="tree", root_vertex=1)
assert str(excinfo.value) == "The tree layout must be used with trees"