From 1085220b67289c010e64ce77e04060d1549f51a1 Mon Sep 17 00:00:00 2001 From: bibin Date: Thu, 22 Sep 2022 22:59:36 +0200 Subject: [PATCH] initial dfs animation --- graph/dfs/dfs-manim.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 graph/dfs/dfs-manim.py diff --git a/graph/dfs/dfs-manim.py b/graph/dfs/dfs-manim.py new file mode 100644 index 0000000..e6018d2 --- /dev/null +++ b/graph/dfs/dfs-manim.py @@ -0,0 +1,35 @@ +from manim import * + + +class Main(Scene): + def construct(self): + # change + adj_list = [[1, 2], [0, 3], [0, 3], [1, 2, 4], [3]] + vertices = [v for v, _ in enumerate(adj_list)] + edges = [(v, u) for v, neighs in enumerate(adj_list) for u in neighs] + + adj_vis = Graph(vertices, edges, labels=True, layout='spring', vertex_config={"fill_color": WHITE, "fill_opacity": 0.5}) + + self.play(Create(adj_vis)) + self.wait() + + visited = [False for _ in adj_list] + + print(adj_vis[0]) + + def dfs(v): + self.play(Indicate(adj_vis[v])) + if visited[v]: + return + + visited[v] = True + self.play(ApplyMethod(adj_vis[v].set_opacity, 1.0)) + + for u in adj_list[v]: + self.play(Indicate(adj_vis.edges[(v, u)]), Indicate(adj_vis.edges[(u, v)])) + dfs(u) + + + self.play(ApplyMethod(adj_vis[v].set_color, BLUE)) + + dfs(0)