API

PageRank

graphflow.pagerank.GooglePageRank.CalculatePageRank(digraph: DiGraph, beta: float, eps: float = 0.0001, maxstep: int = 1000) dict[str, float][source]

Calculate PageRank for a directed graph.

This function computes the PageRank scores for nodes in a directed graph. It first computes the Google Matrix and then calculates the PageRank using the specified parameters.

Parameters:
  • digraph (networkx.DiGraph) – The directed graph for which to compute PageRank.

  • beta (float) – The damping factor (between 0 and 1). Typically set to 0.85.

  • eps (float, optional) – The convergence threshold. The algorithm stops when the change in vectors is less than this value. Default is 1e-4.

  • maxstep (int, optional) – The maximum number of iterations to perform. Default is 1000.

Returns:

A dictionary mapping node identifiers to their PageRank scores.

Return type:

dict

graphflow.pagerank.GooglePageRank.CalculatePageRankFromAdjacencyMatrix(adjMatrix: nptyping.NDArray.(nptyping.Shape.*, *, nptyping.Float), nodes: dict[str, int], eps: float = 0.0001, maxstep: int = 1000, language: ~graphflow.PageRankLanguage = PageRankLanguage.CYTHON) dict[str, float][source]

Calculate PageRank from an adjacency matrix using specified implementation language.

This function computes the PageRank scores for nodes in a graph represented by an adjacency matrix. It can use either Cython or Python implementation based on the language parameter.

Parameters:
  • adjMatrix (numpy.ndarray) – The adjacency matrix representing the graph structure.

  • nodes (dict) – A dictionary mapping node identifiers to their indices.

  • eps (float, optional) – The convergence threshold. The algorithm stops when the change in vectors is less than this value. Default is 1e-4.

  • maxstep (int, optional) – The maximum number of iterations to perform. Default is 1000.

  • language (PageRankLanguage, optional) – The implementation language to use. Default is PageRankLanguage.CYTHON.

Returns:

A dictionary mapping node identifiers to their PageRank scores.

Return type:

dict

graphflow.pagerank.GooglePageRank.CalculatePageRankFromAdjacencyMatrix_Python(adjMatrix: nptyping.NDArray.(nptyping.Shape.*, *, nptyping.Float), nodes: dict[str, int], eps: float = 0.0001, maxstep: int = 1000) dict[str, float][source]

Calculate PageRank from an adjacency matrix using Python implementation.

This function computes the PageRank scores for nodes in a graph represented by an adjacency matrix. It uses an iterative approach until convergence or maximum steps are reached.

Parameters:
  • adjMatrix (numpy.ndarray) – The adjacency matrix representing the graph structure.

  • nodes (dict) – A dictionary mapping node identifiers to their indices.

  • eps (float, optional) – The convergence threshold. The algorithm stops when the change in vectors is less than this value. Default is 1e-4.

  • maxstep (int, optional) – The maximum number of iterations to perform. Default is 1000.

Returns:

A dictionary mapping node identifiers to their PageRank scores.

Return type:

dict

graphflow.pagerank.GooglePageRank.GoogleMatrix(digraph: DiGraph, beta: float) Float), dict[str, int]][source]

Compute the Google Matrix for a directed graph.

The Google Matrix is used in PageRank calculations and represents the probability transition matrix of a random walk on the graph. It incorporates a damping factor to handle dangling nodes and ensure convergence.

Parameters:
  • digraph (networkx.DiGraph) – The directed graph for which to compute the Google Matrix.

  • beta (float) – The damping factor (between 0 and 1). Typically set to 0.85.

Returns:

A tuple containing: - A (numpy.ndarray): The Google Matrix. - nodedict (dict): A dictionary mapping node identifiers to their indices.

Return type:

tuple

Resistance

Get the resistance distance matrix of a simple undirected network. See: http://en.wikipedia.org/wiki/Resistance_distance

class graphflow.simvoltage.resistancedist.GraphResistanceDistance(nodes: list[str] = None, edges: list[tuple[str, str]] = None)[source]

Compute the resistance distance matrix of a simple undirected network.

This class calculates the resistance distance between all pairs of nodes in an undirected graph using the Moore-Penrose pseudoinverse of the Laplacian matrix. Resistance distance is a measure of how well-connected two nodes are in a network, considering all possible paths between them.

See: http://en.wikipedia.org/wiki/Resistance_distance

__init__(nodes: list[str] = None, edges: list[tuple[str, str]] = None)[source]

Initialize the GraphResistanceDistance class.

Parameters:
  • nodes (list, optional) – List of node identifiers. Default is [‘Stephen’, ‘Sinnie’, ‘Elaine’].

  • edges (list of tuples, optional) – List of edges as tuples (node1, node2). Default is [(‘Stephen’, ‘Sinnie’), (‘Elaine’, ‘Sinnie’), (‘Elaine’, ‘Stephen’)].

calculateAdjacencyMatrix() sparse.SparseArray[source]

Calculate the adjacency matrix of the graph.

The adjacency matrix is a square matrix where the entry at position (i, j) is 1 if there is an edge between nodes i and j, and 0 otherwise.

Returns:

The adjacency matrix as a sparse DOK (Dictionary of Keys) matrix.

Return type:

sparse.SparseArray

calculateDegreeMatrix() sparse.SparseArray[source]

Calculate the degree matrix of the graph.

The degree matrix is a diagonal matrix where each diagonal entry represents the degree of the corresponding node (number of edges connected to it).

Returns:

The degree matrix as a sparse DOK (Dictionary of Keys) matrix.

Return type:

sparse.SparseArray

computeResistanceDistance() sparse.SparseArray[source]

Compute the resistance distance matrix for the graph.

This method calculates the resistance distance between all pairs of nodes using the Moore-Penrose pseudoinverse of the Laplacian matrix.

Returns:

The resistance distance matrix as a sparse DOK matrix.

Return type:

sparse.SparseArray

getResistance(node1: str, node2: str) float[source]

Get the resistance distance between two nodes.

Parameters:
  • node1 (str) – The identifier of the first node.

  • node2 (str) – The identifier of the second node.

Returns:

The resistance distance between the two nodes.

Return type:

float

Raises:

UnknownNodeException – If either node is not in the graph.

initializeClass(nodes: list[str], edges: list[tuple[str, str]]) None[source]

Initialize the class with nodes and edges.

Parameters:
  • nodes (list[str]) – List of node identifiers.

  • edges (list of tuples) – List of edges as tuples (node1, node2).

class graphflow.simvoltage.SocialNetworkSimVoltage.SocialNetworkSimVoltage(nodes: list[str] = None, edges: list[tuple[str, str, float]] = None, precalculated_distance: bool = True)[source]

Simulate voltage in a social network to compute resistance distances.

This class models a social network as an electrical circuit where edges have resistance values. It computes the effective resistance between any two nodes using circuit simulation techniques.

__init__(nodes: list[str] = None, edges: list[tuple[str, str, float]] = None, precalculated_distance: bool = True)[source]

Initialize the SocialNetworkSimVoltage class.

Parameters:
  • nodes (list[str], optional) – List of node identifiers. Default is [‘Stephen’, ‘Sinnie’, ‘Elaine’].

  • edges (list of tuples, optional) –

    List of edges as tuples (node1, node2, weight). Default is [(‘Stephen’, ‘Sinnie’, 0.2), (‘Sinnie’, ‘Stephen’, 0.2), (‘Sinnie’, ‘Elaine’, 0.3),

    (‘Elaine’, ‘Sinnie’, 0.2), (‘Stephen’, ‘Elaine’, 1.1), (‘Elaine’, ‘Stephen’, 1.2)].

  • precalculated_distance (bool, optional) – Whether to precalculate distances. Default is True.

average_VR(node: str, volDict: dict[str, float]) tuple[float, float][source]

Compute the average voltage-to-resistance ratio for a node.

This method calculates the sum of voltage-over-resistance values for all connected nodes and the total reciprocal resistance, which are used to compute the new voltage potential for the node.

Parameters:
  • node (str) – The node for which to compute the average voltage-to-resistance ratio.

  • volDict (dict) – A dictionary mapping node identifiers to their voltage values.

Returns:

A tuple containing: - sumVOverR (float): The sum of voltage-over-resistance values. - numRecR (float): The total reciprocal resistance.

Return type:

tuple

checkPersonIrrelevant(person: str, person1: str, person2: str) bool[source]

Check if a person is irrelevant for the path between two other people.

This method determines if a person is not on any shortest path between person1 and person2, making them irrelevant for the resistance calculation.

Parameters:
  • person (str) – The person to check for relevance.

  • person1 (str) – The first person in the path.

  • person2 (str) – The second person in the path.

Returns:

True if the person is irrelevant (not on any shortest path), False otherwise.

Return type:

bool

compute_incurrent(node: str, volDict: dict[str, float]) float[source]

Compute the total current flowing into a node.

This method calculates the sum of currents flowing into a node from all its predecessors where the predecessor has a higher voltage potential.

Parameters:
  • node (str) – The node for which to compute incoming current.

  • volDict (dict) – A dictionary mapping node identifiers to their voltage values.

Returns:

The total incoming current to the node.

Return type:

float

compute_outcurrent(node: str, volDict: dict[str, float]) float[source]

Compute the total current flowing out of a node.

This method calculates the sum of currents flowing out of a node to all its successors where the node has a higher voltage potential.

Parameters:
  • node (str) – The node for which to compute outgoing current.

  • volDict (dict) – A dictionary mapping node identifiers to their voltage values.

Returns:

The total outgoing current from the node.

Return type:

float

constructSocialNetwork(nodes: list[str], edges: list[tuple[str, str, float]]) None[source]

Construct the social network as a directed graph.

This method creates a NetworkX DiGraph from the provided nodes and edges.

Parameters:
  • nodes (list[str]) – List of node identifiers.

  • edges (list of tuples) – List of edges as tuples (node1, node2, weight).

drawNetwork() None[source]

Draw the social network using NetworkX.

This method visualizes the social network graph using NetworkX’s drawing functions.

getResistance(person1: str, person2: str, printVol: bool = False) float[source]

Compute the resistance distance between two people in the social network.

This method simulates voltage distribution in the network with person1 at 1.0V and person2 at 0.0V, then calculates the effective resistance between them.

Parameters:
  • person1 (str) – The first person (at 1.0V potential).

  • person2 (str) – The second person (at 0.0V potential).

  • printVol (bool, optional) – Whether to print voltage values during iteration. Default is False.

Returns:

The resistance distance between the two people.

Return type:

float

initializeClass(nodes: list[str], edges: list[tuple[str, str, float]]) None[source]

Initialize the class with nodes and edges.

Parameters:
  • nodes (list[str]) – List of node identifiers.

  • edges (list of tuples) – List of edges as tuples (node1, node2, weight).

initloop(person1: str, person2: str) dict[str, float][source]

Initialize voltage values for all nodes in the network.

This method sets initial voltage values for each node based on their distance from person1 and person2. Person1 is set to 1.0V, person2 to 0.0V, and other nodes are assigned values based on their relative distances.

Parameters:
  • person1 (str) – The person at 1.0V potential.

  • person2 (str) – The person at 0.0V potential.

Returns:

A dictionary mapping node identifiers to their initial voltage values.

Return type:

dict

precalculate_distance() dict[tuple[str, str], float][source]

Precalculate the shortest path distances between all pairs of nodes.

This method computes the shortest path length between all pairs of nodes in the network and stores them in a dictionary for quick access. If no path exists between two nodes, the distance is set to infinity.

HITS (Hyperlink-Induced Topic Search)

graphflow.hits.hitsrank.CalculateHITS(digraph: DiGraph, eps: float = 0.0001, maxstep: int = 1000) tuple[nptyping.NDArray.(nptyping.Shape.*, nptyping.Float), nptyping.NDArray.(nptyping.Shape.*, nptyping.Float)][source]

Compute the HITS (Hyperlink-Induced Topic Search) algorithm on a NetworkX digraph.

This function calculates the hub and authority scores for each node in a directed graph using the HITS algorithm. It converts the graph to an adjacency matrix and then applies the HITS algorithm.

Parameters:
  • digraph (networkx.DiGraph) – The directed graph on which to compute the HITS algorithm.

  • eps (float, optional) – The convergence threshold. The algorithm stops when the change in vectors is less than this value. Default is 1e-4.

  • maxstep (int, optional) – The maximum number of iterations to perform. Default is 1000.

Returns:

A tuple containing: - hubdict (dict): A dictionary mapping node identifiers to their hub scores. - authdict (dict): A dictionary mapping node identifiers to their authority scores.

Return type:

tuple

graphflow.hits.hitsrank.hits(adjMatrix: nptyping.NDArray.(nptyping.Shape.*, *, nptyping.Float), eps: float = 0.0001, maxstep: int = 1000) tuple[nptyping.NDArray.(nptyping.Shape.*, nptyping.Float), nptyping.NDArray.(nptyping.Shape.*, nptyping.Float)][source]

Compute the HITS (Hyperlink-Induced Topic Search) algorithm on an adjacency matrix.

This function calculates the hub and authority vectors for a given adjacency matrix using the HITS algorithm. The algorithm iteratively computes these vectors until convergence or the maximum number of steps is reached.

Parameters:
  • adjMatrix (numpy.ndarray) – The adjacency matrix representing the graph structure.

  • eps (float, optional) – The convergence threshold. The algorithm stops when the change in vectors is less than this value. Default is 1e-4.

  • maxstep (int, optional) – The maximum number of iterations to perform. Default is 1000.

Returns:

A tuple containing: - hub vector (numpy.ndarray): The hub scores for each node. - authority vector (numpy.ndarray): The authority scores for each node.

Return type:

tuple