H = addedge(G,s,t)
adds an edge to graph G between nodes s and
t. If a node specified by s or
t is not present in G, then that node is
added. The new graph, H, is equivalent to G,
but includes the new edge and any required new nodes.

H = addedge(G,EdgeTable)
adds edges with attributes specified by the table, EdgeTable. The
EdgeTable input must be able to be concatenated with
G.Edges.

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]

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.

Input graph, specified as either a graph or digraph
object. Use graph to create an undirected graph or
digraph to create a directed graph.

Example: G = graph(1,2)

Example: G = digraph([1 2],[2 3])

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:1

Vector

Example:[1 2 3]

Node name

Character vector

Example:'A'

Cell array of character vectors

Example:{'A' 'B' 'C'}

String scalar

Example:"A"

String array

Example:["A" "B" "C"]

Categorical array

Example:categorical("A")

Categorical array

Example:categorical(["A" "B" "C"])

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 in
s and t.

If w is a column vector, then it must have the
same length as s(:) and
t(:).

If w is a matrix, then it must have the same
number of elements as s(:) and
t(:).

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
variable Weight.

If the graph has other edge attributes, then
EdgeTable must contain all of the same
variables as G.Edges to ensure
compatibility.

The order of variables in EdgeTable must be the
same as that of G.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.

Extended Capabilities

C/C++ Code Generation Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

The node pairs s and t 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 or
digraph 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

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.