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

29 lines
773 B
Python
Raw Normal View History

2022-09-22 22:59:36 +02:00
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)
2022-10-14 12:52:46 +02:00
dfs(0)
2022-09-22 22:59:36 +02:00
2022-10-14 12:52:46 +02:00
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)