MGraph¶
Qualified name: manim\_dsa.m\_graph.m\_graph.MGraph
- class MGraph(graph, nodes_position={}, style=<manim_dsa.constants.MGraphStyle._DefaultStyle object>)[source]¶
-
Manim Graph: a class for visualizing the graph data structure using the Manim animation engine.
- Parameters:
graph (
GraphType) – The graph representation, which can be weighted or unweighted. Can be: -list[list[str]]ordict[str, list[str]]for unweighted graph -list[list[tuple[str, str | int]]]ordict[str, list[tuple[str, str | int]]]for weighted graphnodes_position (dict[str,
Vector3D], optional) – A dictionary mapping node labels to their positions as 3D vectors. Defaults to an empty dict.style (
MGraphStyle._DefaultStyle, optional) – The style configuration to be applied to the graph. Defaults toMGraphStyle.DEFAULT.
Methods
Adds a new curved edge between two nodes in the graph.
Adds a new edge between two nodes in the graph.
Adds a label to the graph with specified alignment and buffer.
Adds a new node to the graph with a specified name and position.
Applies a specified layout algorithm to arrange the nodes in the graph.
This method iterates through all the edges in the graph and applies the specified highlight color and stroke width.
This method iterates through all the nodes in the graph and applies the specified highlight color and stroke width.
show_backward_edgeInherited Attributes
animateUsed to animate the application of any method of
self.colordepthThe depth of the mobject.
fill_colorIf there are multiple colors (for gradient) this returns the first one
heightThe height of the mobject.
n_points_per_curvesheen_factorstroke_colorwidthThe width of the mobject.
- class CurvedEdge(line, node1, node2, arrow, start_distance, node_angle=1.0471975511965976, arc_angle=1.0471975511965976)[source]¶
Bases:
EdgeRepresents a curved edge in the graph, connecting two nodes with an arc.
- Parameters:
line (
ArcBetweenPoints) – The ArcBetweenPoints object representing the edge.node1 (
Circle) – The start node (circle) of the edge.node2 (
Circle) – The destination node (circle) of the edge.arrow (
ArrowTriangleFilledTiporNone) – The arrow tip to be added to the edge, if any.node_angle (float, optional) – The angle between the line connecting the nodes and the direction of the arc. Defaults to
PI/3.arc_angle (float, optional) – The angle of the arc between the two nodes. Defaults to
PI/3.start_distance (float)
- weighted(label, label_distance=0.3)[source]¶
Assigns a label (the weight) to the edge and positions it relative to the edge.
- Parameters:
- Returns:
The instance of the
MGraph.Edgewith the applied highlight.ssigned label.- Return type:
self
- class Edge(line, start, end, arrow)[source]¶
Bases:
VGroup,Highlightable,ABCAn abstract class that represents an edge in the graph.
- Parameters:
line (
LineorArcBetweenPoints) – The line or arc that visually represents the edge between two nodes.start (
Point3D) – The starting point of the edge.end (
Point3D) – The ending point of the edge.arrow (
ArrowTriangleFilledTiporNone) – The arrow tip to be added to the edge, if any.
- highlight(stroke_color=ManimColor('#FC6255'), stroke_width=8)[source]¶
Highlights the edge with a specified color and stroke width.
- Parameters:
stroke_color (
ManimColor, optional) – The color to be used for highlighting the edge. Defaults toRED.stroke_width (float, optional) – The width of the stroke used for highlighting. Defaults to
8.
- Returns:
The instance of the
MGraph.Edgewith the applied highlight.- Return type:
self
- is_weighted()[source]¶
Checks if the edge is weighted by examining the presence of a label.
- Returns:
Trueif the edge has a label (indicating it is weighted),Falseotherwise.- Return type:
- set_highlight(stroke_color=ManimColor('#FC6255'), stroke_width=8)[source]¶
Sets the highlight properties for the edge.
- Parameters:
stroke_color (
ManimColor, optional) – The color to be used for highlighting the edge. Defaults toRED.stroke_width (float, optional) – The width of the stroke used for highlighting. Defaults to
8.
- Returns:
The instance of the
MGraph.Edgewith the applied highlight.- Return type:
self
- weighted(label)[source]¶
Assigns a label to the edge, indicating that it is weighted.
- Parameters:
label (
Text) – The label to be assigned to the edge, representing its weight or any other relevant information.- Returns:
The instance of the
MGraph.Edgewith the applied highlight.- Return type:
self
- class Node(circle, label)[source]¶
Bases:
VGroup,HighlightableA class that represents a node (or vertex) of the graph.
- Parameters:
- class StraightEdge(line, node1, node2, arrow, start_distance)[source]¶
Bases:
EdgeRepresents a straight edge in the graph, connecting two nodes with a straight line.
- Parameters:
line (
Line) – The Line object representing the edge.node1 (
Circle) – The start node (circle) of the edge.node2 (
Circle) – The destination node (circle) of the edge.arrow (
ArrowTriangleFilledTiporNone) – The arrow tip to be added to the edge, if any.start_distance (float)
- weighted(label, label_distance=0.3)[source]¶
Assigns a label (the weight) to the edge and positions it relative to the edge.
- Parameters:
- Returns:
The instance of the
MGraph.Edgewith the applied highlight.ssigned label.- Return type:
self
- add_curved_edge(node1_name, node2_name, weight=None, label_distance=0.3, node_angle=1.0471975511965976, arc_angle=1.0471975511965976)[source]¶
Adds a new curved edge between two nodes in the graph.
- Parameters:
node1_name (str) – The name of the first node.
node2_name (str) – The name of the second node.
weight (float, optional) – The weight of the edge. If not provided, the edge will be unweighted.
label_distance (float, optional) – The distance from the edge where the label should be placed. Defaults to
0.2.node_angle (float, optional) – The start angle of the arc between the two nodes. Defaults to
PI/3.arc_angle (float, optional) – The angle of the arc between the two nodes. Defaults to
PI/3.
- Returns:
The updated instance of the
MGraphwith the new curved edge added.- Return type:
self
- add_edge(node1_name, node2_name, weight=None, label_distance=0.2)[source]¶
Adds a new edge between two nodes in the graph.
- Parameters:
node1_name (str) – The name of the first node.
node2_name (str) – The name of the second node.
weight (float, optional) – The weight of the edge. If not provided, the edge will be unweighted.
label_distance (float, optional) – The distance from the edge where the label should be placed. Defaults to
0.2.
- Returns:
The updated instance of the
MGraphwith the new edge added.- Return type:
self
- add_label(text, direction=array([0., 1., 0.]), buff=0.5, **kwargs)[source]¶
Adds a label to the graph with specified alignment and buffer.
- Parameters:
text (
Text) – The label text to be added to the graph.direction (
Vector3D, optional) – The direction in which the label should be positioned relative to the graph. Defaults toUP.buff (float, optional) – The distance between the graph and the label. Defaults to
0.5.**kwargs – Additional keyword arguments that are passed to the function next_to() of the underlying add_label method.
- Returns:
The updated instance of the
MGraphwith the label added.- Return type:
self
- add_node(name, position=array([0., 0., 0.]))[source]¶
Adds a new node to the graph with a specified name and position.
- node_layout(layout='kamada_kawai_layout')[source]¶
Applies a specified layout algorithm to arrange the nodes in the graph.
- Parameters:
layout (str, optional) – The name of the layout algorithm to be applied to the nodes. Defaults to ‘kamada_kawai_layout’. Other common layout options may include ‘spring_layout’, ‘circular_layout’, ‘shell_layout’, and others supported by the underlying graph library. A full list of available layouts can be found in the NetworkX documentation: https://networkx.org/documentation/stable/reference/drawing.html#module-networkx.drawing.layout
- Returns:
The updated instance of the
MGraphwith nodes arranged according to the specified layout.- Return type:
self
- set_edges_highlight(color=ManimColor('#FC6255'), width=8)[source]¶
This method iterates through all the edges in the graph and applies the specified highlight color and stroke width.