# How Hub Nodes and Bridge Nodes Identify Architectural Hotspots in Code Review Graphs

> Discover how hub nodes and bridge nodes identify architectural hotspots. Learn to detect code symbols with high centrality and critical dependencies in your codebase.

- Repository: [Tirth Kanani/code-review-graph](https://github.com/tirth8205/code-review-graph)
- Tags: how-to-guide
- Published: 2026-07-20

---

**Hub nodes and bridge nodes identify architectural hotspots by measuring total degree centrality and betweenness centrality respectively, revealing symbols that either coordinate widespread dependencies or serve as critical bridges between isolated code communities.**

In the `tirth8205/code-review-graph` repository, these complementary graph analysis techniques automatically surface high-risk modification points before they cause systemic failures. By combining **degree-based hub detection** with **path-based bridge detection**, developers can prioritize code reviews and testing efforts on the most architecturally significant—and potentially fragile—parts of their codebase.

## How Hub Nodes Identify High-Impact Dependencies

Hub nodes represent symbols that act as coordination centers within your codebase. These nodes accumulate many incoming dependencies (high in-degree) or dispatch many outgoing calls (high out-degree), making them dangerous points to modify without understanding their full impact.

### Total Degree Calculation Logic

The hub detection algorithm resides in `find_hub_nodes` within [[`code_review_graph/analysis.py`](https://github.com/tirth8205/code-review-graph/blob/main/code_review_graph/analysis.py)](https://github.com/tirth8205/code-review-graph/blob/main/code_review_graph/analysis.py#L14-L55). The function iterates over every edge in the `GraphStore`, incrementing separate counters for `in_degree` and `out_degree` while explicitly excluding file-type nodes from consideration.

For each non-file node, the algorithm computes `total_degree = in_degree + out_degree`. It then sorts all nodes by this metric and returns the top *N* results. The output includes critical metadata: the symbol's `name`, `qualified_name`, `kind`, file location, individual degree counts, and its assigned `community_id` from the graph's community detection pipeline.

### Why Hubs Matter for Architectural Risk

A hub node indicates **high blast radius**—modify this symbol, and you potentially break dozens of dependent modules or calling contexts. In [`code_review_graph/tools/analysis_tools.py`](https://github.com/tirth8205/code-review-graph/blob/main/code_review_graph/tools/analysis_tools.py), the `get_hub_nodes_func` wrapper (lines 17-42) exposes this analysis as an MCP tool, automatically suggesting follow-up actions like `get_impact_radius` to quantify exactly how many downstream components rely on the identified hub.

## How Bridge Nodes Expose Structural Chokepoints

While hubs reveal dense connection centers, bridge nodes expose **structural vulnerabilities**—symbols that sit on the shortest paths between otherwise disconnected code communities.

### Betweenness Centrality Computation

The `find_bridge_nodes` function in [[`code_review_graph/analysis.py`](https://github.com/tirth8205/code-review-graph/blob/main/code_review_graph/analysis.py)](https://github.com/tirth8205/code-review-graph/blob/main/code_review_graph/analysis.py#L58-L112) materializes the graph as a NetworkX object via `store._build_networkx_graph()`. It calculates **betweenness centrality** using NetworkX's optimized algorithms.

For performance, the implementation applies a threshold at lines 77-81: it computes exact centrality for graphs with 5,000 or fewer nodes, but switches to approximate sampling for larger codebases. The function filters out file-type nodes, rounds scores to six decimal places for consistency, and returns results sorted in descending order.

### Cross-Community Connectivity Risks

A bridge node with high betweenness centrality links distinct architectural communities (clusters). If such a node fails or changes its interface, it can **partition the codebase**, severing communication paths between major subsystems. The `get_bridge_nodes_func` wrapper (lines 47-66 in [`analysis_tools.py`](https://github.com/tirth8205/code-review-graph/blob/main/analysis_tools.py)) surfaces these chokepoints alongside their `community_id`, prompting reviewers to ask: "Why does this single function connect communities A and B?"

## Practical Implementation in code-review-graph

The analysis pipeline integrates these detection methods into reusable tools that operate against any repository's graph store.

### Core Analysis Functions

| Function | Location | Purpose |
|----------|----------|---------|
| `find_hub_nodes` | [`code_review_graph/analysis.py`](https://github.com/tirth8205/code-review-graph/blob/main/code_review_graph/analysis.py) (lines 14-55) | Calculates total degree for all non-file symbols |
| `find_bridge_nodes` | [`code_review_graph/analysis.py`](https://github.com/tirth8205/code-review-graph/blob/main/code_review_graph/analysis.py) (lines 58-112) | Computes betweenness centrality with exact/approximate switching |
| `get_hub_nodes_func` | [`code_review_graph/tools/analysis_tools.py`](https://github.com/tirth8205/code-review-graph/blob/main/code_review_graph/tools/analysis_tools.py) (lines 17-42) | MCP tool wrapper for hub detection |
| `get_bridge_nodes_func` | [`code_review_graph/tools/analysis_tools.py`](https://github.com/tirth8205/code-review-graph/blob/main/code_review_graph/tools/analysis_tools.py) (lines 47-66) | MCP tool wrapper for bridge detection |

### Running Hub and Bridge Detection

Import the analysis tools directly to surface architectural hotspots in your current working tree:

```python
from code_review_graph.tools.analysis_tools import get_hub_nodes_func

result = get_hub_nodes_func(top_n=5)
for hub in result["hub_nodes"]:
    print(
        f"{hub['name']} ({hub['qualified_name']}) – "
        f"total_degree={hub['total_degree']}, community={hub['community_id']}"
    )

```

For bridge detection:

```python
from code_review_graph.tools.analysis_tools import get_bridge_nodes_func

result = get_bridge_nodes_func(top_n=5)
for bridge in result["bridge_nodes"]:
    print(
        f"{bridge['name']} ({bridge['qualified_name']}) – "
        f"betweenness={bridge['betweenness']}, community={bridge['community_id']}"
    )

```

Both functions return dictionaries containing `next_tool_suggestions`, recommending subsequent actions such as `get_impact_radius` (to measure a hub's blast radius) or `detect_changes` (to verify if a bridge node was modified in recent commits).

## Summary

- **Hub nodes** identify architectural hotspots using **total degree centrality** (in-degree + out-degree) in `find_hub_nodes`, revealing symbols with widespread dependency networks.
- **Bridge nodes** identify hotspots using **betweenness centrality** in `find_bridge_nodes`, exposing critical connectors between code communities that risk partitioning the architecture if modified.
- The [`code_review_graph/analysis.py`](https://github.com/tirth8205/code-review-graph/blob/main/code_review_graph/analysis.py) implementation optimizes bridge calculations by switching between exact and approximate algorithms based on graph size (≤5,000 nodes).
- Both analyses exclude file-type nodes to focus on functional symbols (functions, classes, modules) and include community context to understand cross-module relationships.
- MCP tool wrappers in [`analysis_tools.py`](https://github.com/tirth8205/code-review-graph/blob/main/analysis_tools.py) provide CLI-accessible interfaces that suggest follow-up impact analysis tools.

## Frequently Asked Questions

### What is the difference between a hub node and a bridge node?

A **hub node** measures how many direct connections a symbol has (total degree), identifying centralized logic that many parts of the codebase depend upon. A **bridge node** measures how often a symbol appears on shortest paths between other nodes (betweenness centrality), identifying critical links between otherwise separate code communities. Hubs represent coordination centers; bridges represent structural chokepoints.

### How does the code-review-graph handle large repositories when calculating bridge nodes?

The `find_bridge_nodes` function in [`code_review_graph/analysis.py`](https://github.com/tirth8205/code-review-graph/blob/main/code_review_graph/analysis.py) automatically switches between exact and approximate betweenness centrality calculations based on graph size. For repositories with 5,000 or fewer nodes, it computes exact centrality; for larger graphs, it uses approximate sampling to maintain performance without sacrificing analytical utility.

### Why are file-type nodes excluded from hub and bridge detection?

File nodes are excluded to ensure the analysis focuses on **functional architectural elements**—functions, classes, and modules—rather than organizational container nodes. This prevents high-degree file nodes from obscuring the true structural hotspots within the source code, ensuring reviewers identify risky symbols rather than merely large files.

### How can developers use hub and bridge node detection during code reviews?

Developers can invoke `get_hub_nodes_func` and `get_bridge_nodes_func` via the MCP tools to generate pre-review reports highlighting high-risk symbols. For hub nodes, reviewers should verify comprehensive test coverage before accepting changes. For bridge nodes, reviewers should assess whether the cross-community linkage is intentional or represents architectural technical debt requiring refactoring to reduce coupling between modules.