directed graphand the previous one introducedundirected graphIt's basically the same, the only difference isdirected graphThe edges of have directionality, which represents a unidirectional or dependent relationship between vertices.
directed graphG
Generally expressed as:G=<V,E>
. As with undirected graphs, theV
is the set of vertices ofE
is the set of edges.
The difference is thatundirected graphis enclosed in parentheses(V,E)
,directed graphangle brackets < ><V,E>
。
existdirected graphin which the edges are oriented, so that, from theVertex A to Vertex BThe side of the edge with the edge from theVertex B to Vertex AThe side ofnot the sameThe.
together withundirected graphSame.directed graphThere are also many application scenarios, for example:
existMap navigationMiddle.directed graphOften used to represent road networks.
Nodes represent locations (e.g., intersections, cities, etc.), directed edges represent roads, and the weights of the edges can indicate the length of the road, travel time, or traffic conditions.
existchain managementIn this, a directed graph can be used to represent the flow path of goods.
The nodes represent the links in the supply chain (e.g., suppliers, manufacturers, distributors, etc.), the edges represent the paths through which the goods flow, and the capacity of the edges can represent the carrying capacity of the goods.
existsocial networkIn the previous article, it was mentioned that buddy relationships between users can be represented by undirected graphs.
And directed graphs can also be used in social network analysis, it can be used to represent the attention relationship between users, retweeting relationship, etc., used to analyze the user's behavioral patterns.
following introductionmanim
Plottingdirected graphtarget audienceDiGraph
。
1. Main parameters
directed graph object (math)DiGraph
The main parameters are similar to the undirected graph:
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, thevertices
cap (a poem)edges
The related parameters (e.g. xxx_type, xxx_config) are better understood.
labels
parameter sets whether the label of the vertex needs to be displayed or not, the default is to put thevertices
value as the content of the label.
layout
The parameters have a variety of ready-made layouts built in:
'circular',
'kamada_kawai'
'partite'
'planar'
'random'
'shell'
'spectral'
'spiral'
'spring'
'tree'
layout_config
Parameters can be fine-tuned for the above ready-made layout.
The last two parametersparitions
cap (a poem)root_vertex
More specific.
paritions
can only be found inlayout
set to'partite'
used when generating layered graphs (such as those describing neural networks), the
paritions
Used to set which vertices each layer contains;
root_vertex
can only be found inlayout
set to'tree'
when used for tree diagrams.
root_vertex
Used to set the root node of the tree.
The examples that follow demonstrate how to use theparitions
cap (a poem)root_vertex
to generatebedded (geology)cap (a poem)tree-like(used form a nominal expression)directed graph。
2. Main approaches
directed graphDiGraph
methods are mainly used to dynamically change directed graphs, such as adding or removing vertices and edges.
name (of a thing) | clarification |
---|---|
add_edges | Adding edges to a directed graph |
add_vertices | Adding vertices to a directed graph |
remove_edges | Deleting edges of a directed graph |
remove_vertices | Delete the vertices of a directed graph |
change_layout | The structure of a dynamically modified directed graph |
from_networkx | through (a gap)networkx to generate a directed graph |
networkx
is another commonly usedPython
library for creating, manipulating, and studying the structure of complex networks.
DiGraph
objects can also be used directly based on thenetworkx
of the object generation graph.
3. Examples of use
The following example is similar to the previous example of an undirected graph, except that it uses a directed graph insteadDiGraph
object to implement it.
3.1. Vertex configuration
The setup of vertices is almost the same as for undirected graphs.
# Different color settings
graph = DiGraph(
vertex_config={
0: {"color": RED}, # ...
# ...
},
)
# Vertex display label
graph = DiGraph(
labels=True, )
)
# Star Vertices
graph = DiGraph(
vertex_config={"outer_radius": 0.15}, vertex_type=Star, }
vertex_type=Star, )
)
3.2 Configuration of edges
Like vertices, edges of directed graphs can have color, thickness, and other attributes.
The difference with an undirected graph is that the edges of a directed graph can be styled with arrows.
# Color of the edge
graph = DiGraph(
edge_config={
(0, 1): {"color": RED},
# ...
},
)
# Thickness of the edge
graph = DiGraph(
edge_config={
(0, 1): {"stroke_width": 1},
# ...
},
)
# Sides with different arrows
graph = DiGraph(
edge_config={
(0, 1): {
"tip_config": {
"tip_shape": ArrowCircleTip,
},
},
(0, 2): {
"tip_config": {
"tip_shape": ArrowTriangleTip,
},
},
# ...
},
)
3.3. Built-in layout
directed graphThe built-in layout is the same as the one described in the previous article on undirected graphs.
for layout in [
"spring",
"circular",
"kamada_kawai",
"planar",
"random",
"shell",
"spectral",
"spiral",
]:
graph = DiGraph(
layout=layout,
)
3.4 Layer diagrams
The layout of the cascade diagram needs to be coupled with the parameterpartitions
Used together, thepartitions
in determining which vertices are in each layer.
Directed graphs have edges that have directions and are plotted more like the structure of a neural network.
partitions = [[0, 1], [2, 3, 4], [5, 6], [7, 8]]
graph = DiGraph(
layout="partite",
partitions=partitions,
)
3.5. Tree diagrams
The layout of the tree diagram needs to work with the parametersroot_vertex
Used together, theroot_vertex
defines the tree'sroot vertexWhich one is it.
This is the same place asundirected graphOne difference is that when drawing a directed tree graph, the order of vertices and edges is important, and you need to start at the root node and pass in each vertex in turn.
In the following example, the second dendrogram changes the root node, not just theroot_vertex
Just do it, you need to change the order of the vertices in the graph first.
The code below is abbreviated, the full code can be downloaded from the link in the last part of the text.
# Initial tree
graph = DiGraph(
layout="tree",
root_vertex=0, )
)
# Important!!!!
# The order of nodes and edges needs to be adjusted before modification
# Modify the root node
graph2 = DiGraph(
layout="tree",
root_vertex=2, )
)
4. Annexes
The complete code for the article is on a web disk (),
Download at.Full Code (Access code: 6872)