addedge

Adds edges to a graph or digraph, creating new nodes if needed.

Category: Mathematics > Graph and Network Algorithms

Syntax

H = addedge(G, s, t)

Adds an edge from node s(k) to node t(k) for every k.

H = addedge(G, s, t, w)

Also sets the edge weights to w.

Description

Graph objects are values, so addedge returns the modified copy and you normally write G = addedge(G, ...). A node index larger than numnodes(G) adds the missing nodes. When the graph has node names, an unknown name in s or t creates a new node with that name.

If G carries weights, pass w for the new edges. Edges of an undirected graph are kept in sorted order, so new edges may not end up at the end of G.Edges.

Input arguments

G
A graph or digraph.
s, t
Source and target nodes, as indices or as node names. Scalars or vectors of equal length.
w
Edge weights, one per new edge.

Output arguments

H
A new graph with the added edges. G itself is not modified.

Example

Add an edge and a weighted edge

G = graph([1 2], [2 3]);
G = addedge(G, 1, 3);
disp(G.Edges.EndNodes)
W = addedge(graph([1 2], [2 3], [1 1]), 3, 4, 9);
disp(W.Edges.Weight')
Output
     1     2
     1     3
     2     3
     1     1     9

See also

rmedge addnode graph digraph findedge