Main Content

degree

Degree of graph nodes

Description

example

D= degree(G)returns the degree of each node in graphG. The degree is the number of edges connected to each node.

example

D= degree(G,nodeIDs)returns the degree of the nodes specified bynodeIDs.

Examples

collapse all

Create and plot a graph, and then usedegreeto find the degree of each node in the graph.

s = [1 1 1 4 4 6 6 6]; t = [2 3 4 5 6 7 8 9]; G = graph(s,t); plot(G)

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

deg = degree(G)
deg =9×13 1 1 3 1 4 1 1 1

deg(j)indicates the degree of nodej.

Create and plot a graph, and then find the degree of the first, third, and fifth nodes.

s = {'a''a''a''d''d''f''f''f'}; t = {'b''c''d''e''f''g''h''i'}; G = graph(s,t); plot(G)

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

nodeIDs = {'a''c''e'}'; deg = degree(G,nodeIDs)
deg =3×13 1 1

deg(j)indicates the degree of nodenodeIDs(j).

Input Arguments

collapse all

Input graph, specified as agraphobject. Usegraphto create an undirected graph object.

Example:G = graph(1,2)

Node identifiers, specified as one or more node indices or node names.

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:D = degree(G,[3 4])

Example:D = degree(G,{'LAX','ALB'})

Output Arguments

collapse all

Degree of nodes, returned as a numeric array.Dis a column vector unless you specifynodeIDs, in which caseDhas the same size asnodeIDs.

A node that is connected to itself by an edge (a self-loop) is listed as its own neighbor only once, but the self-loop adds 2 to the total degree of the node.

Extended Capabilities

版本历史

Introduced in R2015b