Main Content


Number of edges between two nodes



C = edgecount(G,s,t) returns the number of edges between the source and target node pairs s and t in graph G.


collapse all

Create a graph and display the edges table for reference. Find the number of edges between node 1 and node 2.

G = graph([1 1 1 1 2 2],[2 2 2 3 4 5]);
ans=6×1 table

     1    2 
     1    2 
     1    2 
     1    3 
     2    4 
     2    5 

N = edgecount(G,1,2)
N = 3

You also can specify a single source node and several target nodes. Find the number of edges between node 1 and all other nodes (including self-loops).

N = edgecount(G,1,1:numnodes(G))
N = 5×1


Input Arguments

collapse all

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

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. s and t must specify the same number of nodes, unless one of them specifies a single node.

This table shows the different ways to refer to one or more nodes either by their numeric node indices or by their node names.

FormSingle NodeMultiple Nodes
Node index


Example: 1


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: edgecount(G,1,2)

Example: edgecount(G,1,1:5)

Example: edgecount(G,{'a' 'b' 'c'},'a')

Extended Capabilities

Version History

Introduced in R2018a