soi-manim/graph/dfs/dfs-manim.py

29 lines
773 B
Python

from manim import *
class Main(Scene):
def construct(self):
adj_list = [[1, 2], [0, 3], [0, 3], [1, 2, 4], [3]]
visited = [False for _ in adj_list]
def dfs(v):
if visited[v]:
return
visited[v] = True
for u in adj_list[v]:
dfs(u)
dfs(0)
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').move_to(3*LEFT)
code = Code(file_name="dfs-manim.py", language="python").move_to(3*RIGHT).scale(0.5)
#self.play(Create(adj_vis), Create(code))
#self.wait()
self.add(adj_vis, code)