Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add weighted edges and self loop edges to graphs #3945

Open
wants to merge 40 commits into
base: main
Choose a base branch
from

Conversation

ZynoXelek
Copy link

@ZynoXelek ZynoXelek commented Oct 4, 2024

Overview: What does this pull request change?

This pull request addresses issue #3153. It makes it possible to create self-loop edges that are correctly rendered using curved lines (by default) as edges. It also adds the possibility to set weights (or any other label) to the edges.

Motivation and Explanation: Why and how do your changes improve the library?

As it is explained in the linked issue, these changes make graphs ready for representation of state-machines.

Links to added or changed documentation pages

Further Information and Comments

How it looks like:

For a regular Graph:
GraphScene_ManimCE_v0 18 1

For a DiGraph:
GraphScene_ManimCE_v0 18 1

Comments:
The three added private methods (in the GenericGraph class) may be moved to another location. Though the objective here was to make it as general as possible without creating new classes that would support these new features. This way, it can be used in all future graphs.

A label can be added to a specific graph edge individually by passing a weights dictionary when creating the graph. It supports SingleStringMathTex, Text and Tex objects as well as any other object that can be casted to str.
Additional configuration is possible, globally or locally, by passing a "weight_config" dictionary in the edge_config dictionary.

The labels have the same color as their respective edges, with their background color being set to config["background_color"] by default. This behavior is customizable by passing a text_color or background_color parameter in the weight_config dictionary. The user can also use a custom label type instead of the default LabelledDot class as a weight_type parameter in the the weight_config dictionary. To achieve this, the custom class must be initializable with the label and color keywords (If the class inherits from MObject, the color keyword is already supported).

Self-loops edges are easily created by defining a (v, v) edge in the edges dictionary. Just as with other edges, self-loop edges use the edge_type parameter during initialization. Analogously, this class has to support the path_arc keyword.
They also support additional configuration by passing a "loop_config" dictionary in the edge_config. It can either be a local or global configuration. It supports "path_arc" and "angle_between_points" keywords. They are respectively used to determine the angle spanned by the drawn arc, and the angle between the two end points of the arc (relative to the center of the vertex).

Possible future features:
It could be interesting to add a simpler way of displaying two-sided edges in DiGraph which could have different weights. It is possible to do it with the current implementation, but weights are hard to read, and if the two edges are created with different colors, the result is aesthetically unappealing.

Reviewer Checklist

  • The PR title is descriptive enough for the changelog, and the PR is labeled correctly
  • If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
  • If applicable: newly added functions and classes are tested

@ZynoXelek ZynoXelek marked this pull request as draft October 6, 2024 12:51
@ZynoXelek
Copy link
Author

Support for bidirectional edges have been removed from this PR since it was not related to the original issue.
It could be added back in another PR if asked for.

Here is how it could look like:
GraphScene_ManimCE_v0 18 1

@ZynoXelek ZynoXelek marked this pull request as ready for review October 7, 2024 23:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🆕 New
Development

Successfully merging this pull request may close these issues.

1 participant