Main Content

findedge

Locate edge in graph

Description

example

[sOut,tOut] = findedge(G)returns the source and target node IDs,sOutandtOut, for all of the edges in graphG.

example

[sOut,tOut] = findedge(G,idx)finds the source and target nodes of the edges specified byidx.

example

idxOut= findedge(G,s,t)returns the numeric edge indices,idxOut, for the edges specified by the source and target node pairssandt. The edge indices correspond to the rowsG.Edges.Edge(idxOut,:)in theG.Edgestable of the graph. If there are multiple edges betweensandt, then all their indices are returned. An edge index of0indicates an edge that is not in the graph.

[idxOut,m] = findedge(G,s,t)additionally returns a vectormindicating which node pair(s,t)is associated with each edge index inidxOut. This is useful when there are multiple edges between the same two nodes.

Examples

collapse all

Create a graph, and then determine the edge index for the (1,2) and (3,5) edges.

s = [1 1 2 2 2 3 3 3]; t = [2 3 3 4 5 6 7 5]; G = graph(s,t)
G = graph with properties: Edges: [8x1 table] Nodes: [7x0 table]
idxOut = findedge(G,[1 3],[2 5])
idxOut =2×11 - 6

idxOutcontains the row index intoG.Edges.EndNodesfor each specified edge.

Create a graph, and then determine the end nodes of all edges in the graph.

s = {'a''a''b''b''c''c'}; t = {'b''c''d''e''f''g'}; G = graph(s,t); G.Edges
ans=6×1 tableEndNodes ______________ {'a'} {'b'} {'a'} {'c'} {'b'} {'d'} {'b'} {'e'} {'c'} {'f'} {'c'} {'g'}
[sOut,tOut] = findedge(G)
sOut =6×11 1 2 2 3 3
兜售=6×12 3 4 5 6 7

Create a graph, and then determine the end nodes for the edges whose indices are3and7.

s = [1 1 1 1 2 2 3 3 4 4]; t = [2 3 4 5 6 7 8 9 10 11]; G = digraph(s,t)
G = digraph with properties: Edges: [10x1 table] Nodes: [11x0 table]
[sOut,tOut] = findedge(G,[3 7])
sOut =2×11 3
兜售=2×14 8

Create a graph.

s = [1 1 2 3]; t = [2 3 3 4]; weights = [10 20 30 40]; G = graph(s,t,weights)
G = graph with properties: Edges: [4x2 table] Nodes: [4x0 table]

Find the weight of the (1,3) edge, usingfindedgeto retrieve the index.

G.Edges.Weight(findedge(G,1,3))
ans = 20

Usefindedgeto change the weights of several multigraph edges.

Create and plot a multigraph. This graph has two edges between node 2 and node 4.

s = [1 1 2 3 2 2]; t = [2 3 3 4 4 4]; weights = [10 20 30 40 10 10]; G = graph(s,t,weights); plot(G,'EdgeLabel',G.Edges.Weight)

Figure contains an axes object. The axes object contains an object of type graphplot.

Change the weights of the edges between nodes (3,2) and (2,4). Specify two outputs tofindedgeto get the end-node indices,m. This output is useful when there are multiple edges between two nodes, sinceidxOutcan have more elements than the number of node pairs insandt. The edgeidxOut(1) = 3connects the node pair(s(1),t(1)) = (3,2), and the edgesidxOut(2) = 4andidxOut(3) = 5connect the edge(s(2),t(2)) = (2,4).

s = [3 2]; t = [2 4]; w = [1 4]; [idxOut, m] = findedge(G, s, t)
idxOut =3×13 4 5
m =3×11 2 2
G.Edges.Weight(idxOut) = w(m); plot(G,'EdgeLabel',G.Edges.Weight)

Figure contains an axes object. The axes object contains an object of type graphplot.

Input Arguments

collapse all

输入图,specified as either agraphordigraphobject. Usegraphto create an undirected graph ordigraphto create a directed graph.

Example:G = graph(1,2)

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

Node pairs, specified as separate arguments of node indices or node names. Similarly located elements insandtspecify the source and target nodes for edges in the graph.

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"]

分类数组

Example:categorical("A")

分类数组

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

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

Example:G = findedge(G,{'a' 'a'; 'b' 'c'},{'b' 'c'; 'c' 'e'})

Edge indices, specified as a scalar or vector of positive integers. The edge index corresponds to a row in theG.Edgestable of the graph,G.Edges(idx,:).

Output Arguments

collapse all

Edge indices, returned as a scalar or vector of nonnegative integers. The edge indices correspond to rows in theG.Edgestable of the graph,G.Edges(idxOut,:). An edge index of0indicates an edge that is not in the graph.

The length ofidxOutcorresponds to the number of node pairs in the input, unless the input graph is a multigraph.

End node indices, returned as a vector. The values inmconnect the edge indices inidxOutto the input node pairs(s,t). The edgeidxOut(j)connects the node pair with indexm(j).

Node IDs, returned as separate scalars or vectors of positive integers. Similarly located elements insOutandtOutspecify the source and target nodes that form the edgesG.Edges(idx,:).

Version History

Introduced in R2015b