Skip to main content

union_find

docs-source

Abstract#

Analysis of connected components is a common task in graph analytics.

By using a disjoint-set data structure that keeps track of them, the algorithm implemented in this module enables the user to quickly check whether a pair of given nodes is in the same or different connected component. A check on a pair of nodes is effectively executed in O(1) time.

The implementation of the disjoint-set data structure and its operations uses the union by rank and path splitting optimizations described in "Worst-case Analysis of Set Union Algorithms" 1, and presented with examples here.

1 Worst-case Analysis of Set Union Algorithms, Robert E. Tarjan and Jan van Leeuwen

TraitValue
Module typemodule
ImplementationPython
Graph directionundirected
Edge weightsunweighted
Parallelismsequential

Procedures#

connected(nodes1, nodes2, mode, update)#

Input:#

  • nodes1: Union[Vertex, List[Vertex]] ➡ First value (or list thereof) in connectedness calculation.
  • nodes2: Union[Vertex, List[Vertex]] ➡ Second value (or list thereof) in connectedness calculation.
  • mode: str("pairwise") ➡ Mode of combining nodes1 and nodes2. Can be p or pairwise for a pairwise product, or c or cartesian for a Cartesian product of the arguments. Pairwise by default.
  • update: bool(True) ➡ Specifies whether the disjoint-set data structure should be reinitialized. Enabled by default. If the graph has been modified since the previous call of this procedure, turning update off ensures that the changes are not visible in the output.

Output:#

  • node1: Vertex ➡ Node in nodes1.
  • node2: Vertex ➡ Node in nodes2.
  • connected: boolTrue if the above nodes are in the same connected component of the graph.

Usage:#

MATCH (m:Node)WITH collect(m) AS nodes1MATCH (n:Node)WITH collect(n) AS nodes2, nodes1CALL union_find.connected(nodes1, nodes2) YIELD *RETURN node1, node2, connected;

Example#