addedge
Add new edge to graph
Description
Examples
Add Edges to Graph
Add two new edges to an existing graph. Since one of the new edges references a node that does not exist, addedge
automatically adds the required fourth node to the graph.
G = graph([1 2],[2 3])
G = graph with properties: Edges: [2x1 table] Nodes: [3x0 table]
G = addedge(G,[2 1],[4 3])
G = graph with properties: Edges: [4x1 table] Nodes: [4x0 table]
Add Weighted Edges to Graph
Create a directed graph with weighted edges.
G = digraph({'A' 'B' 'C'}, {'D' 'C' 'D'}, [10 20 45])
G = digraph with properties: Edges: [3x2 table] Nodes: [4x1 table]
Add three new weighted edges to the graph. addedge
also automatically adds any required new nodes to the graph.
G = addedge(G, {'A' 'D' 'E'}, {'E' 'B' 'D'}, [5 30 5])
G = digraph with properties: Edges: [6x2 table] Nodes: [5x1 table]
Add Edges with Attributes to Graph
Create a graph whose edges have the attributes Weight
and Power
. Use an edge table to create the graph.
EdgeTable = table([1 2; 2 3; 2 4; 2 5; 5 6; 5 7; 5 8], ... {'on','off','off','on','on','on','off'}',[10 20 20 10 10 10 20]', ... 'VariableNames',{'EndNodes','Power','Weight'}); G = graph(EdgeTable)
G = graph with properties: Edges: [7x3 table] Nodes: [8x0 table]
Add two new edges to the graph by creating a smaller table that can be concatenated to G.Edges
. Note that this smaller table must use the same order of variables as G.Edges
.
NewEdges = table([5 9; 3 6], {'on' 'off'}', [10 20]', ... 'VariableNames',{'EndNodes','Power','Weight'}); G = addedge(G,NewEdges)
G = graph with properties: Edges: [9x3 table] Nodes: [9x0 table]
View the new edge list of the graph, which includes the added edges.
G.Edges
ans=9×3 table
EndNodes Power Weight
________ _______ ______
1 2 {'on' } 10
2 3 {'off'} 20
2 4 {'off'} 20
2 5 {'on' } 10
3 6 {'off'} 20
5 6 {'on' } 10
5 7 {'on' } 10
5 8 {'off'} 20
5 9 {'on' } 10
Input Arguments
s,t
— Node pairs (as separate arguments)
node indices | node names
Node pairs, specified as separate arguments of node indices or node names.
Similarly located elements in s
and t
specify the source and target nodes for edges in the graph.
If you add edges using node names that are not already present in the
graph, then addedge
adds the new node names to the
bottom of the g.Nodes.Name
table. If s
and t
are categorical arrays, then the categories of
s
and t
are used as node names.
This can include categories that are not elements in s
or
t
.
This table shows the different ways to refer to one or more nodes either by their numeric node indices or by their node names.
Form | Single Node | Multiple Nodes |
---|---|---|
Node index | Scalar Example: | Vector Example: |
Node name | Character vector Example: | Cell array of character vectors Example: |
String scalar Example: | String array Example: | |
Categorical array Example:
| Categorical array Example:
|
Example: G = addedge(G, [1 2], [3 4])
adds two edges to
the graph: one from node 1
to node 3
,
and one from node 2
to node
4
.
Example: G = addedge(G, {'a' 'a'; 'b' 'c'}, {'b' 'c'; 'c'
'e'})
adds four edges to the graph, the first of which goes
from 'a'
to 'b'
.
w
— Edge weights
scalar | vector | matrix
Edge weights, specified as a scalar, vector, or matrix.
If
w
is a scalar or row vector, then it is scalar expanded to specify a weight for each edge ins
andt
.If
w
is a column vector, then it must have the same length ass(:)
andt(:)
.If
w
is a matrix, then it must have the same number of elements ass(:)
andt(:)
.
Example: G = addedge(G, [2 2], [4 5], [1 100]')
adds two
edges with weights of 1
and
100
.
Data Types: single
| double
Complex Number Support: Yes
EdgeTable
— Edge attributes
table
Edge attributes, specified as a table. If you do not specify
s
and t
to define the graph edges
being added, then the first variable in EdgeTable
is
required to be a two-column matrix called EndNodes
that
defines the graph edges being added.
EdgeTable
must have these general properties:
For weighted graphs,
EdgeTable
must contain a variableWeight
.If the graph has other edge attributes, then
EdgeTable
must contain all of the same variables asG.Edges
to ensure compatibility.The order of variables in
EdgeTable
must be the same as that ofG.Edges
.
Data Types: table
Output Arguments
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
The node pairs
s
andt
must be node indices. Node names are not supported.You cannot add new variables or new columns to the
G.Edges
table after the graph object is created.When you construct a
graph
ordigraph
object in MATLAB® and pass it to a MEX function generated using MATLAB Coder™, you cannot add edges to the graph object.The edge properties must be data types that can be stored as variable-size arrays in code generation. For example, the data type cannot be any of these:
a string array
a cell array with different sizes on each cell
a cell array of character vectors converted using
cellstr
a user-defined class
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
Version History
Introduced in R2015bR2019a: Support for categorical node names
Support added for categorical node names as inputs. This enables you to use data that is imported as categorical to create a graph, without the need for data type manipulation.
R2018a: Change in handling of duplicate edges
graph
, digraph
, and
addedge
no longer produce errors when they encounter
duplicate edges. Instead, the duplicate edges are added to the graph and the result
is a multigraph. The ismultigraph
function is useful to detect this situation, and
simplify
provides an easy way to remove the extra edges.
Comando de MATLAB
Ha hecho clic en un enlace que corresponde a este comando de MATLAB:
Ejecute el comando introduciéndolo en la ventana de comandos de MATLAB. Los navegadores web no admiten comandos de MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)