Graph-Based Edge Weighting Algorithms Using Cluster Centroid Cosine Similarity

Mathematical Modeling of Semantic Graph Topologies

Modern algorithmic search architectures evaluate contextual site hierarchy not merely through flat lexical matching, but via complex semantic manifolds. When modeling an enterprise content ecosystem as a directed graph $G = (V, E)$, the nodes $V$ represent discrete documents (URLs), and the directed edges $E$ represent internal hyperlinks. The primary challenge in programmatic site design is determining the optimal subset of edges $E subset V times V$ and their corresponding transition weights $w_{ij}$ to maximize PageRank distribution, topical authority consolidation, and thematic relevance.

To construct these edges algorithmically, vector embeddings of individual documents are mapped to continuous dense vector spaces $mathbb{R}^d$. While raw document-to-document cosine similarity can produce basic bipartite graphs, it fails to account for macro-topical hierarchy and leads to chaotic link distribution when scaled across tens of thousands of URLs. Integrating a machine-learning-driven Programmatic Internal Linking Architecture Based on Cluster Affinity Scores requires computing edge weights not just on pairwise vector distances, but through compound affinity scoring that incorporates cluster centroid vectors.

By extracting foundational groupings from a high-dimensional keyword cluster tool, enterprise content graphs can formalize dense sub-graphs (clusters) where directional edge weighting reflects both micro-relevance (document-to-document) and macro-relevance (document-to-centroid and centroid-to-centroid affinities).

Computing Cluster Centroid Vectors

Let an arbitrary document $d_i in V$ be mapped to a normalized embedding vector $mathbf{x}_i in mathbb{R}^d$ such that $|mathbf{x}_i|_2 = 1$. The corpus $V$ is partitioned into $K$ disjoint clusters $mathcal{C} = {C_1, C_2, dots, C_K}$, where each document belongs to a specific cluster $C_k$.

Unweighted Geometric Centroid

The unweighted centroid vector $mathbf{c}_k$ of cluster $C_k$ is the geometric mean of the document embeddings within that partition:

$$mathbf{c}_k = frac{1}{|C_k|} sum_{d_i in C_k} mathbf{x}_i$$

To preserve directional invariance under Euclidean inner products, the centroid must be $L_2$-normalized:

$$mathbf{hat{c}}_k = frac{mathbf{c}_k}{|mathbf{c}_k|_2}$$

Feature-Weighted Centroid

In production information retrieval systems, documents within a cluster contribute unequally to topical authority. Let $omega_i$ denote a composite scalar representing document importance, derived from a combination of query impressions, historical organic clicks, and target query volume extracted via a keyword cluster tool pipeline. The authority-weighted centroid is defined as:

$$mathbf{c}_k^* = sum_{d_i in C_k} omega_i mathbf{x}_i quad text{where} quad sum_{d_i in C_k} omega_i = 1$$

$$mathbf{hat{c}}_k^* = frac{mathbf{c}_k^*}{|mathbf{c}_k^*|_2}$$

Formulating Centroid-Modulated Cosine Similarity

Standard cosine similarity between two document vectors $mathbf{x}_i$ and $mathbf{x}_j$ measures local alignment:

$$S_{text{doc}}(mathbf{x}_i, mathbf{x}_j) = frac{mathbf{x}_i cdot mathbf{x}_j}{|mathbf{x}_i|_2 |mathbf{x}_j|_2} = mathbf{x}_i cdot mathbf{x}_j quad (text{for normalized vectors})$$

However, relying solely on $S_{text{doc}}$ introduces high-variance inter-cluster edge creation: two documents sharing localized vocabulary may yield an artificially high $S_{text{doc}}$ despite residing in orthogonal conceptual domains. To eliminate these “semantic drift” anomalies, we introduce Cluster Centroid Cosine Similarity metrics.

Document-to-Centroid Alignment

The alignment of document $d_i in C_a$ with an external target cluster $C_b$ measures the affinity of that document to the target cluster’s macro-topic:

$$S_{text{d2c}}(mathbf{x}_i, mathbf{hat{c}}_b) = mathbf{x}_i cdot mathbf{hat{c}}_b$$

Centroid-to-Centroid Affinity

The structural affinity between two clusters $C_a$ and $C_b$ establishes the theoretical maximum semantic conductance across the boundary separating them:

$$S_{text{c2c}}(mathbf{hat{c}}_a, mathbf{hat{c}}_b) = mathbf{hat{c}}_a cdot mathbf{hat{c}}_b$$

Edge Weighting Formulation Algorithms

To construct directed edge weights $w_{ij} in [0, 1]$ from node $d_i in C_a$ to node $d_j in C_b$, we define a multi-tiered weighting engine that bifurcates based on graph partition topology.

Case 1: Intra-Cluster Edge Weighting ($a = b$)

Within a unified topical partition, edge generation focuses on establishing cluster cohesion without circular cannibalization. The edge weight combines document similarity with the target document’s proximity to the cluster core:

$$w_{ij}^{(text{intra})} = alpha cdot (mathbf{x}_i cdot mathbf{x}_j) + (1 – alpha) cdot (mathbf{x}_j cdot mathbf{hat{c}}_a)$$

Where $alpha in [0, 1]$ is a tunable tuning coefficient (empirically optimized at $alpha approx 0.65$). A higher weight is allocated to targets that sit closer to the cluster centroid, preventing edge proliferation around outlier nodes.

Case 2: Inter-Cluster Edge Weighting ($a neq b$)

When bridging distinct partitions, the algorithm must penalize cross-cluster transitions unless both the source document, the target document, and their respective cluster centroids demonstrate tight alignment:

$$w_{ij}^{(text{inter})} = (mathbf{x}_i cdot mathbf{x}_j) times left( mathbf{hat{c}}_a cdot mathbf{hat{c}}_b right)^{gamma} times sigmaleft(beta left[(mathbf{x}_i cdot mathbf{hat{c}}_b) – tauright]right)$$

Where:

  • $gamma ge 1$ is the partition penalty exponent, which exponentially suppresses links between divergent high-level categories.
  • $sigma(z) = frac{1}{1 + e^{-z}}$ is the standard sigmoid function acting as a soft gatekeeper.
  • $tau$ is a similarity threshold parameter derived dynamically from the corpus-wide centroid similarity distribution.
  • $beta$ dictates the steepness of the gating activation.

Graph Sparsification and Transition Probability Matrix

A dense adjacency matrix $W = [w_{ij}]_{N times N}$ containing continuous weights across millions of document pairs creates crawling inefficiencies and dilutes computational link equity. The raw affinity graph must undergo rigorous sparsification before ingestion into the link deployment engine.

$k$-Nearest Neighbor and $epsilon$-Neighborhood Sparsification

The sparsified directed adjacency matrix $tilde{W}$ is derived by applying combined local and global thresholds:

$$tilde{w}_{ij} = begin{cases}
w_{ij} & text{if } w_{ij} ge tau_{text{global}} text{ and } d_j in text{TopK}(d_i, k_{text{out}})
0 & text{otherwise}
end{cases}$$

Here, $k_{text{out}}$ caps the maximum out-degree of any given node, preventing anchor-text dilution, while $tau_{text{global}}$ guarantees an absolute baseline of semantic affinity.

Row Stochastic Normalization for Topic-Sensitive PageRank

To analyze how topical authority cascades through this engineered topology, the sparsified weight matrix $tilde{W}$ is normalized into a row-stochastic transition probability matrix $P$:

$$P_{ij} = frac{tilde{w}_{ij}}{sum_{k=1}^N tilde{w}_{ik}}$$

For dangling nodes where $sum_{k=1}^N tilde{w}_{ik} = 0$, probabilities are evenly distributed back across the source node’s parent cluster centroid nodes:

$$P_{ik} = frac{1}{|C_a|} quad forall d_k in C_a$$

Algorithmic Implementation: Centroid Edge-Weight Pipeline

The entire vector-to-graph transformation can be implemented within an automated ETL pipeline. Below is the operational pseudo-code demonstrating the construction of the directed edge weights:

import numpy as np
from sklearn.metrics.pairwise import cosine_similarity

def compute_centroid_edge_matrix(
    document_vectors: np.ndarray,      # Shape: (N, D) - Normalized
    cluster_labels: np.ndarray,        # Shape: (N,) - Cluster IDs
    alpha: float = 0.65,
    gamma: float = 2.0,
    k_out: int = 8,
    tau: float = 0.45
) -> np.ndarray:
    N, D = document_vectors.shape
    unique_clusters = np.unique(cluster_labels)
    K = len(unique_clusters)
    
    # 1. Compute and normalize centroids
    centroids = np.zeros((K, D))
    for idx, c in enumerate(unique_clusters):
        members = document_vectors[cluster_labels == c]
        c_mean = np.mean(members, axis=0)
        centroids[idx] = c_mean / np.linalg.norm(c_mean)
        
    # 2. Pairwise Cosine Similarities
    S_doc = np.dot(document_vectors, document_vectors.T)
    S_c2c = np.dot(centroids, centroids.T)
    S_d2c = np.dot(document_vectors, centroids.T)
    
    W = np.zeros((N, N))
    
    # 3. Vectorized Edge Weight Computation
    for i in range(N):
        c_i = cluster_labels[i]
        c_i_idx = np.where(unique_clusters == c_i)[0][0]
        
        for j in range(N):
            if i == j:
                continue
                
            c_j = cluster_labels[j]
            c_j_idx = np.where(unique_clusters == c_j)[0][0]
            
            s_doc_ij = S_doc[i, j]
            
            if c_i == c_j:
                # Intra-cluster weighting
                s_target_centroid = S_d2c[j, c_i_idx]
                w_ij = alpha * s_doc_ij + (1.0 - alpha) * s_target_centroid
            else:
                # Inter-cluster weighting
                s_centroids = S_c2c[c_i_idx, c_j_idx]
                s_source_to_target_cluster = S_d2c[i, c_j_idx]
                
                # Sigmoid gating
                gate = 1.0 / (1.0 + np.exp(-10.0 * (s_source_to_target_cluster - tau)))
                w_ij = s_doc_ij * (s_centroids ** gamma) * gate
                
            W[i, j] = w_ij

    # 4. Sparsification: Top-K filtering per row
    W_sparse = np.zeros_like(W)
    for i in range(N):
        top_indices = np.argsort(W[i, :])[-k_out:]
        for idx in top_indices:
            if W[i, idx] > 0.0:
                W_sparse[i, idx] = W[i, idx]
                
    return W_sparse

Graph Entropy and Topical Leakage Mitigation

A structural failure mode in programmatic architectures is topical leakage, which occurs when an excessive number of low-confidence inter-cluster edges bleed link equity out of high-performing clusters into semantically distant sections of the website. To diagnose and prevent this, we calculate the graph entropy $H(G_k)$ of each cluster partition:

$$H(G_k) = – sum_{d_i in C_k} sum_{d_j notin C_k} P_{ij} log_2 (P_{ij} + epsilon)$$

When the topical entropy $H(G_k)$ exceeds a predetermined boundary threshold, the system executes an automated pruning step: the exponent $gamma$ on inter-cluster connections is incrementally increased by an offset $Delta gamma = 0.5$, dynamically suppressing outgoing boundary edges until internal graph cohesion recovers.

By coupling centroid-anchored cosine similarities with rigorous thresholding and entropy constraints, search engineering teams can deploy programmatic internal links with deterministic control over crawl paths, contextual relevance, and link equity transfer across large-scale web graphs.

Related Guides in This Series

In-Depth Sub-Topics & Exploration

Similar Posts

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *