Location>code7788 >text

manim learning-by-doing - undirected graphs

Popularity:63 ℃/2024-10-10 12:04:23

undirected graphIt belongs to the math in thegraph theoryThis discipline.

what is calledundirected graphG, which is the set of verticesV(non-empty sets) and edge setsE(the set of unordered bipartite groups consisting of elements in V) consisting of graphs that

can be expressed asG=(V,E)

existundirected graphin which the edges have no direction, i.e., fromVertex A to Vertex BThe side of the edge with the edge from theVertex B to Vertex Aof the edges are the same.

undirected graphSimple and intuitive, it is often used to describe social networks, transportation networks and electronic circuits.

existsocial networkingin which users can be considered as vertices and relationships between users can be considered as edges.

Because friendships are usually two-way, with no clear direction, they're perfect for using theundirected graphto indicate.

existurban transportation planningIn this, the intersections or traffic nodes can be considered as vertices and the roads or traffic routes between the intersections as edges.

Because roads are usually two-way, it is also appropriate to use theundirected graphto indicate.

existElectronic circuit designin which individual electronic components (e.g., resistors, capacitors, inductors, etc.) can be viewed as vertices and the connecting relationships between components (e.g., wire connections) as edges.

undirected graphIt is possible to represent this circuit structure because currents and signals in a circuit are usually transmitted in both directions.

following introductionmanimPlottingundirected graphtarget audienceGraph

1. Main parameters

undirected graph object (computing)GraphThe main parameters are:

Parameter name typology clarification
vertices list List of vertices of the graph
edges list The list of edges of the graph, each
labels dict Whether the vertex displays labeled text
label_fill_color str Background color of the label
layout str Layout of the fixed points in the diagram
layout_config dict Configure how to layout the vertices in the graph
layout_scale float The scale of the graph's individual vertex layouts
vertex_type Mobject The type of the vertex, which does not have to be a point, but can be any other object in manim
vertex_config dict Vertex-related configurations
vertex_mobjects dict A series of vertex objects
edge_type Mobject The type of the edge, which does not have to be a line, but can be any other object in the manim
edge_config dict Edge-related configurations
paritions list
root_vertex dict

Of these parameters, theverticescap (a poem)edgesThe related parameters (e.g. xxx_type, xxx_config) are better understood.

labelsparameter sets whether the label of the vertex needs to be displayed or not, the default is to put theverticesvalue as the content of the label.

layoutThe parameters have a variety of ready-made layouts built in:

  • 'circular',
  • 'kamada_kawai'
  • 'partite'
  • 'planar'
  • 'random'
  • 'shell'
  • 'spectral'
  • 'spiral'
  • 'spring'
  • 'tree'

layout_configParameters can be fine-tuned for the above ready-made layout.

The last two parametersparitionscap (a poem)root_vertexMore specific.

paritionscan only be found inlayoutset to'partite'used when generating layered graphs (such as those describing neural networks), the

paritionsUsed to set which vertices each layer contains;

root_vertexcan only be found inlayoutset to'tree'when used for tree diagrams.

root_vertexUsed to set the root node of the tree.

The examples that follow demonstrate how to use theparitionscap (a poem)root_vertexto generatebedded (geology)cap (a poem)tree-like(used form a nominal expression)undirected graph

2. Main approaches

undirected graphGraphmethods are mainly used to dynamically change the undirected graph, such as adding or removing vertices and edges.

name (of a thing) clarification
add_edges Adding edges to an undirected graph
add_vertices Adding vertices to an undirected graph
remove_edges Deleting edges of an undirected graph
remove_vertices Delete the vertices of an undirected graph
change_layout The structure of a dynamically reformulated undirected graph
from_networkx through (a gap)networkxto generate an undirected graph

networkxis another commonly usedPythonlibrary for creating, manipulating, and studying the structure of complex networks.

GraphThe object can be used directly under thenetworkxof the object generation graph.

3. Examples of use

Here's an exampleGraphThe use of objects.

3.1. Vertex configuration

The vertex defaults toDotobject, you can set its size and color, and you can also add labels, which means displaying text in the vertices.

Going a step further, the settings can change the shape of the vertices using themanimto use other geometric objects in the graph as vertices.

# Different color settings
graph = Graph(
    vertex_config={
        0: {"color": RED}, # ...
        # ...
    },
)

# Vertex display label
graph = Graph(
    labels=True, )
)

# Star Vertices
graph = Graph(
    vertex_config={"outer_radius": 0.15}, vertex_type=Star, )
    vertex_type=Star, )
)

3.2 Configuration of edges

The edges of the undirected graph are also the same as the vertices, you can set the color, thickness and other attributes, in addition to the default straight line can be changed to a dashed line.

# Color of the edge
graph = Graph(
    edge_config={
        (0, 1): {"color": RED},
        # ...
    },
)

# Thickness of the edge
graph = Graph(
    edge_config={
        (0, 1): {"stroke_width": 1},
        # ...
    },
)

# dotted line
graph = Graph(
    edge_type=DashedLine,
)

3.3. Built-in layout

In the above example, we can only control theundirected graph(used form a nominal expression)verticecap (a poem)suffix of a noun of localitythe number of its members, without being able to control its shape and layout.

manimA number of different layouts are built in for us, and using these built-in layouts directly can save a lot of time.

for layout in [
    "spring",
    "circular",
    "kamada_kawai",
    "planar",
    "random",
    "shell",
    "spectral",
    "spiral",
]:
    graph = Graph(
        layout=layout,
    )

3.4 Layer diagrams

The layout of the cascade diagram needs to be coupled with the parameterpartitionsUsed together, thepartitionsin determining which vertices are in each layer.

The structural diagram of a neural network is a typical layer diagram.

In the following example, thepartitionsThere are 4 layers defined in the parameter.

partitions = [[0, 1], [2, 3, 4], [5, 6], [7, 8]]
graph = Graph(
    layout="partite",
    partitions=partitions,
)

3.5. Tree diagrams

The layout of the tree diagram needs to work with the parametersroot_vertexUsed together, theroot_vertexdefines the tree'sroot vertexWhich one is it.

In the following example, first set theroot_vertexvertex (math.)0Then put theroot_vertexChange to vertex2

# The initial tree
graph = Graph(
    layout="tree",
    root_vertex=0, )
)

# Modify the root node
graph2 = Graph(
    graph2 = Graph(
    root_vertex=2, )
)

4. Annexes

The complete code for the article is on a web disk (),

Download at.Full Code (Access code: 6872)