# Centrality Measures in Semantica: Degree, PageRank, and Graph Analysis

> Explore centrality measures in Semantica including Degree, PageRank, and more. Analyze your knowledge graph with powerful built-in tools for deeper insights.

- Repository: [Semantica /semantica](https://github.com/semantica-agi/semantica)
- Tags: deep-dive
- Published: 2026-09-10

---

**Semantica provides five built-in centrality measures—Degree, Betweenness, Closeness, Eigenvector, and PageRank—accessible through the `CentralityCalculator` class in [`semantica/kg/centrality_calculator.py`](https://github.com/semantica-agi/semantica/blob/main/semantica/kg/centrality_calculator.py).**

The semantica-agi/semantica repository ships with a comprehensive knowledge-graph engine that includes native support for graph-theoretic analysis. Understanding the available centrality measures enables developers to quantify node importance, identify key entities, and analyze relationship networks without external dependencies.

## Available Centrality Measures in Semantica

The `CentralityCalculator` class implements a suite of classical graph algorithms. The `supported_centrality_types` list defined at lines 7-13 in [`semantica/kg/centrality_calculator.py`](https://github.com/semantica-agi/semantica/blob/main/semantica/kg/centrality_calculator.py) enumerates each available measure.

### Degree Centrality

**Degree centrality** counts the number of direct connections of a node and normalizes that count by the maximum possible degree in the graph. This metric identifies highly connected "hub" entities within the knowledge graph. Access this measure via the `calculate_degree_centrality()` method.

### Betweenness Centrality

**Betweenness centrality** quantifies how often a node lies on the shortest paths between other node pairs. Nodes with high betweenness act as bridges or gatekeepers within the network structure. Compute this using `calculate_betweenness_centrality()`.

### Closeness Centrality

**Closeness centrality** inversely relates to the average shortest-path distance from a node to all other reachable nodes. This measure identifies entities that can efficiently spread information through the graph. Invoke `calculate_closeness_centrality()` to obtain these scores.

### Eigenvector Centrality

**Eigenvector centrality** scores nodes based on the importance of their neighbors, using power-iteration techniques on the adjacency matrix. This metric captures the influence of well-connected entities beyond just their direct connection counts. Use `calculate_eigenvector_centrality()` for this analysis.

### PageRank

**PageRank** implements Google's random-walk ranking algorithm, evaluating node importance through iterative link-structure analysis with configurable damping factors. This is particularly effective for directed graphs and citation networks. Call `calculate_pagerank()` with optional `damping_factor` (default 0.85) and `max_iterations` parameters.

## Computing Centrality Measures

### Calculating Individual Metrics

Import `CentralityCalculator` from [`semantica/kg/centrality_calculator.py`](https://github.com/semantica-agi/semantica/blob/main/semantica/kg/centrality_calculator.py) and instantiate the calculator to compute specific centrality scores:

```python
from semantica.kg.centrality_calculator import CentralityCalculator

# Load or build a graph in Semantica format (dict with "entities" & "relationships")

graph = {...}

calc = CentralityCalculator()

# Degree centrality

degree = calc.calculate_degree_centrality(graph)
print("Top node by degree:", degree["rankings"][0])

# Betweenness centrality

betweenness = calc.calculate_betweenness_centrality(graph)
print("Node with highest betweenness:", betweenness["rankings"][0])

# Closeness centrality

closeness = calc.calculate_closeness_centrality(graph)
print("Most central (closeness):", closeness["rankings"][0])

# Eigenvector centrality

eigen = calc.calculate_eigenvector_centrality(graph)
print("Highest eigenvector score:", eigen["rankings"][0])

# PageRank with custom parameters

pagerank = calc.calculate_pagerank(
    graph, 
    damping_factor=0.85, 
    max_iterations=30
)
print("Highest PageRank:", pagerank["rankings"][0])

```

### Batch Processing with calculate_all_centrality

The `calculate_all_centrality()` method runs every supported centrality algorithm in a single pass and returns a unified results dictionary. This approach minimizes redundant shortest-path calculations when multiple metrics are required.

```python
from semantica.kg.centrality_calculator import CentralityCalculator

calc = CentralityCalculator()
all_results = calc.calculate_all_centrality(graph)

# Access individual measures from the combined output

degree_top = all_results["centrality_measures"]["degree"]["rankings"][0]
betweenness_top = all_results["centrality_measures"]["betweenness"]["rankings"][0]
closeness_top = all_results["centrality_measures"]["closeness"]["rankings"][0]
eigenvector_top = all_results["centrality_measures"]["eigenvector"]["rankings"][0]

```

## Algorithm Registry and Discovery

The `algorithm_registry` in [`semantica/kg/registry.py`](https://github.com/semantica-agi/semantica/blob/main/semantica/kg/registry.py) (lines 58-63) registers PageRank as a first-class centrality algorithm, making it discoverable through the registry's introspection capabilities. Query registered algorithms programmatically:

```python
from semantica.kg.registry import algorithm_registry

centrality_algos = algorithm_registry.list_category("centrality")
print("Registered centrality algorithms:", centrality_algos)

# Output: ['pagerank']

```

This registration pattern allows the system to treat PageRank as a plugin-capable component while maintaining consistency with Semantica's broader algorithm management framework.

## Summary

- Semantica implements **five centrality measures** through `CentralityCalculator`: Degree, Betweenness, Closeness, Eigenvector, and PageRank.
- Each measure exposes a dedicated `calculate_*()` method in [`semantica/kg/centrality_calculator.py`](https://github.com/semantica-agi/semantica/blob/main/semantica/kg/centrality_calculator.py).
- The `calculate_all_centrality()` convenience method computes all metrics simultaneously for efficiency.
- PageRank is additionally registered in [`semantica/kg/registry.py`](https://github.com/semantica-agi/semantica/blob/main/semantica/kg/registry.py) (lines 58-63) as a discoverable centrality algorithm.
- All methods accept standard Semantica graph dictionaries containing "entities" and "relationships" keys.

## Frequently Asked Questions

### What centrality measures are available in Semantica?

Semantica provides five centrality measures: **Degree**, **Betweenness**, **Closeness**, **Eigenvector**, and **PageRank**. These are implemented in the `CentralityCalculator` class located at [`semantica/kg/centrality_calculator.py`](https://github.com/semantica-agi/semantica/blob/main/semantica/kg/centrality_calculator.py) and enumerated in the `supported_centrality_types` list.

### How do I compute all centrality measures at once?

Call the `calculate_all_centrality()` method on a `CentralityCalculator` instance. This executes all five algorithms in one pass and returns a dictionary with results under the `"centrality_measures"` key, organized by measure name (e.g., `"degree"`, `"betweenness"`, `"pagerank"`).

### What parameters does PageRank support in Semantica?

The `calculate_pagerank()` method accepts `damping_factor` (float, typically 0.85) and `max_iterations` (int) parameters. These control the random-walk probability and convergence limits respectively, matching the standard Google PageRank implementation.

### Where can I find test examples for centrality calculations?

Reference implementations appear in [`tests/kg/test_provenance_workflows.py`](https://github.com/semantica-agi/semantica/blob/main/tests/kg/test_provenance_workflows.py), which exercises each centrality method with provenance tracking, and [`tests/kg/test_real_world_scenarios.py`](https://github.com/semantica-agi/semantica/blob/main/tests/kg/test_real_world_scenarios.py), which demonstrates combined calculations on academic citation networks using real-world knowledge graph structures.