networkx.Graph.degree

property Graph.degree

A DegreeView for the Graph as G.degree or G.degree().

The node degree is the number of edges adjacent to the node. The weighted node degree is the sum of the edge weights for edges incident to that node.

This object provides an iterator for (node, degree) as well as lookup for the degree for a single node.

Parameters
  • nbunch (single node, container, or all nodes (default= all nodes)) – The view will only report edges incident to these nodes.

  • weight (string or None, optional (default=None)) – The name of an edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1. The degree is the sum of the edge weights adjacent to the node.

Returns

  • If a single node is requested

  • deg (int) – Degree of the node

  • OR if multiple nodes are requested

  • nd_view (A DegreeView object capable of iterating (node, degree) pairs)

Examples

>>> G = nx.path_graph(4)  # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.degree[0]  # node 0 has degree 1
1
>>> list(G.degree([0, 1, 2]))
[(0, 1), (1, 2), (2, 2)]