liveBook · Manning

Degree algorithm

As explained in Knowledge Graphs and LLMs in Action

A degree algorithm computes the number of relationships incident to each node in a graph.

A degree algorithm computes how many relationships are incident to each node. In a knowledge graph, ranking nodes by degree exposes highly connected hubs that can distort path-based discovery. section 7.7.4

A practical pipeline uses the result to improve graph retrieval:

  • Create a graph projection containing the relevant ontology nodes and relationships. section 7.7.4
  • Stream degree scores, order nodes by score, and collect the highest-scoring nodes as hubs. section 7.7.4
  • Find candidate paths between target entities, then reject paths containing identified hubs. section 7.7.4

Filtering misleading medical-ontology paths

A SNOMED graph initially produced 11,185 paths between Zika virus disease and Infectious neuronitis. Filtering paths through high-degree hubs reduced the result to nine, though some general entities remained. section 7.7.4

Listing 7.40 Filtering out hub nodes
CALL gds.degree.stream('snomedGraph')
YIELD nodeId, score
WITH gds.util.asNode(nodeId).name AS name, score AS degree
ORDER BY degree DESC
LIMIT 350
WITH collect(name) AS hub_nodes  #1

MATCH (s1), (s2)
WHERE s1.id = "3928002" AND s2.id = "40956001"
WITH s1, 
     s2,
     allShortestPaths((s1)-[:SNOMED_RELATION*1..8]-(s2)) AS paths,
    hub_nodes  #2
UNWIND paths AS path
WITH relationships(path) AS path_edges,
nodes(path) AS path_nodes,
hub_nodes
WITH 
[n IN path_nodes | n.name] AS node_names,
[r IN path_edges | COALESCE(r.type, 'IS_A')] AS rel_types,  #3
[n IN path_edges | startnode(n).name] AS rel_starts,  #4
hub_nodes
WHERE NOT any(x IN node_names WHERE x IN hub_nodes)  #5
WITH [i IN range(0, size(node_names) - 1) |
CASE
WHEN i = size(node_names) - 1
THEN '(' + node_names[i] + ')'  #6
WHEN node_names[i] = rel_starts[i]
THEN '(' + node_names[i] + ')' + '-[:' + rel_types[i] + ']->'  #7
ELSE '(' + node_names[i] + ')' + '<-[:' + rel_types[i] + ']-'  #8
END
] AS string_paths
RETURN DISTINCT apoc.text.join(string_paths, '') AS `Extracted paths`

Inspect the Cypher pipeline that streams degree scores, selects 350 hub nodes, computes paths, and excludes paths containing them. section 7.7.4

How Knowledge Graphs and LLMs in Action covers it

How deep it goes
Explains projection, degree streaming, ranking, and path exclusion, but does not derive the algorithm’s internals.
Examples
Uses SNOMED paths linking Zika virus disease and Infectious neuronitis as the filtering example.
What it assumes
Assumes familiarity with graph nodes, relationships, Cypher queries, and path matching.
What you can do after
Apply it via Neo4j GDS to rank hubs and remove hub-containing paths from graph queries.
How it connects
Connects graph projection, node centrality, shortest paths, knowledge graphs, and ontologies.

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.