liveBook · Manning

Node degree

As explained in Knowledge Graphs and LLMs in Action

Node degree is the number of edges incident to a node, optionally separated by incoming and outgoing direction.

Node degree counts a node’s direct connections. Used as a graph feature, it provides a compact representation of connectivity for classification and other network analyses. section 9.3.1 section 10.1.1

Compute and interpret degree features through a small pipeline:

  • Count a node’s neighbors to obtain its global or total degree. section 10.1.1
  • Split neighbors into meaningful categories when labels are available, such as fraud degree and legitimate degree. section 10.1.1
  • Use the resulting scalar or vector as input to a classifier, while validating whether degree alone captures enough structure. section 9.3.1
  • Discount highly connected intermediate nodes in path-based features when raw path counts would overrepresent generic hubs. section 10.2.2

Karate-club classification

A NetworkX karate-club graph represents each node with a one-value embedding containing its degree. That simple feature performs poorly in the shown classification results, while adding category-specific neighbor degrees substantially improves them. section 9.3.1

Fraud-network metrics

For each node, compute total degree, fraud degree, and legitimate degree from the is_fraudster attribute. A node with many fraudulent direct neighbors receives a different representation from one with equally many legitimate neighbors. section 10.1.1

Listing 10.3 Computing types of node degrees in a fraud detection network
def compute_degree_metrics(G):
    degree_metrics = {}   #1

    for node in G.nodes():
        neighbors = list(G.neighbors(node))
        total_degree = len(neighbors)  #2

        fraud_degree = sum(1 for neighbor in neighbors 
                          if G.nodes[neighbor].get('is_fraudster', False))  #3

        legit_degree = total_degree - fraud_degree   #4

        degree_metrics[node] = {
            'total_degree': total_degree,
            'fraud_degree': fraud_degree,
            'legit_degree': legit_degree
        }

    return degree_metrics  #5

def get_node_degrees(G, node):
    metrics = compute_degree_metrics(G)
    return metrics.get(node, {
        'total_degree': 0,
        'fraud_degree': 0,
        'legit_degree': 0
    })   #6

Inspect the reusable NetworkX-style implementation for calculating total, fraud, and legitimate degrees, including a default result for missing nodes. section 10.1.1

How Knowledge Graphs and LLMs in Action covers it

How deep it goes
Defines neighbor counting and demonstrates code, but stops short of treating degree as a sufficient representation for complex structure.
Examples
Demonstrates karate-club classification, fraud and legitimate neighbor counts, and metformin–disease path weighting.
What it assumes
Assumes basic graphs, nodes, neighbors, and labels while teaching degree computation from graph structure and node attributes.
What you can do after
Apply it via NetworkX and Neo4j to construct connectivity features and reduce hub-driven noise in graph-based analyses.
How it connects
Links node degree to node classification, node embeddings, fraud detection, shortest paths, and degree-weighted path count.

Also explained in

Compare how 4 books explain node degree →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Knowledge Graphs and LLMs in Action. The section links point to the parts of the book it draws on.