PAGERANK

PageRank computes a ranking of the nodes based on the structure of the incoming links. Originally designed to rank web pages, it can be applied to any graph to measure “prestige” or “influence”.

It works by counting the number and quality of links to a node to determine a rough estimate of how important the node is. The underlying assumption is that more important nodes are likely to receive more links from other important nodes.

The function returns a ranked list of nodes and their PageRank scores, sorted from highest to lowest.

Excel Usage

=PAGERANK(edges, alpha, weighted)
  • edges (list[list], required): 2D array of edges [source, target, weight?].
  • alpha (float, optional, default: 0.85): Damping parameter (typical value 0.85).
  • weighted (bool, optional, default: false): If True, use edge weights for calculations.

Returns (list[list]): 2D array of [node, score] pairs, sorted by score descending.

Example 1: Simple path PageRank

Inputs:

edges
A B
B C
C D

Excel formula:

=PAGERANK({"A","B";"B","C";"C","D"})

Expected output:

Result
D 0.370146
C 0.298811
B 0.214888
A 0.116156
Example 2: Cycle PageRank

Inputs:

edges
A B
B C
C A

Excel formula:

=PAGERANK({"A","B";"B","C";"C","A"})

Expected output:

Result
A 0.333333
B 0.333333
C 0.333333
Example 3: Custom damping PageRank

Inputs:

edges alpha
A B 0.5
A C

Excel formula:

=PAGERANK({"A","B";"A","C"}, 0.5)

Expected output:

Result
B 0.357143
C 0.357143
A 0.285714
Example 4: Weighted PageRank

Inputs:

edges weighted
A B 10 true
A C 1

Excel formula:

=PAGERANK({"A","B",10;"A","C",1}, TRUE)

Expected output:

Result
B 0.460449
C 0.279811
A 0.25974

Python Code

import networkx as nx
import scipy

def pagerank(edges, alpha=0.85, weighted=False):
    """
    Calculate the PageRank of the nodes in the graph.

    See: https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.link_analysis.pagerank_alg.pagerank.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        edges (list[list]): 2D array of edges [source, target, weight?].
        alpha (float, optional): Damping parameter (typical value 0.85). Default is 0.85.
        weighted (bool, optional): If True, use edge weights for calculations. Default is False.

    Returns:
        list[list]: 2D array of [node, score] pairs, sorted by score descending.
    """
    try:
        def to2d(x):
            return [[x]] if not isinstance(x, list) else x

        edges = to2d(edges)

        if not isinstance(edges, list) or not edges:
            return "Error: edges must be a non-empty 2D list"

        G = nx.DiGraph() 

        if weighted:
            G.add_weighted_edges_from([(str(row[0]), str(row[1]), float(row[2]) if len(row) >= 3 else 1.0) for row in edges if len(row) >= 2])
            weight_arg = 'weight'
        else:
            G.add_edges_from([(str(row[0]), str(row[1])) for row in edges if len(row) >= 2])
            weight_arg = None

        if not G.nodes:
            return "Error: No valid nodes or edges found in input"

        try:
            scores = nx.pagerank(G, alpha=float(alpha), weight=weight_arg)
        except nx.PowerIterationFailedConvergence:
            return "Error: Power iteration failed to converge"

        # Sort by score descending, then by node name
        sorted_scores = sorted(scores.items(), key=lambda x: (-x[1], x[0]))

        return [[node, float(score)] for node, score in sorted_scores]
    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

2D array of edges [source, target, weight?].
Damping parameter (typical value 0.85).
If True, use edge weights for calculations.