Clique Hierarchy: private API
PortfolioOptimisers.clique3 — Function
clique3(A::MatNum)Computes the list of 3-cliques in a Maximal Planar Graph (MPG).
This function identifies all 3-cliques (triangles) in the adjacency matrix A of a MPG. It returns the candidate cliques, their edge indices, and a matrix listing all unique 3-cliques. Used internally in DBHT and related phylogenetic clustering algorithms.
Algorithm
- Remove the diagonal of
Aand reduce it to a binary matrix. - Form
A2 = A * A, whose entry counts the paths of length two between a pair. - Keep the upper triangle of the entries where
A2andAare both non-zero, givingP. A stored entry ofPis an edge whose two ends share at least one neighbour. - Read the row and the column index of every stored entry of
Pinto the two columns ofE, one row per candidate edge. - For each candidate edge, intersect the neighbourhoods of its two ends, giving
K3[n], the third vertices that close a triangle on it. - Sort each triple
(E[n, 1], E[n, 2], K3[n][m])and append it tocliquewhencliquedoes not already hold it, so a triangle found from each of its three edges is stored once. - Sort the rows of
cliqueon its three columns, and drop the placeholder first row.
Arguments
A:N × Nadjacency matrix of a Maximal Planar Graph (MPG). A non-zero entry is an edge.
Returns
K3::Vector{Vector{Int}}: Vector of vectors, each containing the indices of nodes forming a candidate 3-clique.E::Matrix{Int}: Matrix with nonzero indices and entries of candidate cliques (edge pairs).clique::Matrix{Int}:Nc×3matrix. Each row lists the three vertices of a unique 3-clique in the MPG.
Related
PortfolioOptimisers.FindDisjoint — Function
FindDisjoint(Adj::MatNum, Cliq::VecNum)Finds disjointed cliques in an adjacency matrix.
This function identifies nodes that are not adjacent to a given 3-clique in the adjacency matrix, and classifies all nodes into three groups: members of the clique, nodes in the same connected component as the clique, and nodes in a disjoint component.
Algorithm
- Copy
AdjintoTemp, and collect inIndxNotevery vertex that is not one of the three ofCliq. - Zero the rows and the columns of
TempatCliq, which cuts the clique out of the graph and separates the two sides it was joining. - Run
breadthfromIndxNot[1], givingd, and mark every vertexdleft at an infinity with-1. - Write
1intoTat every vertex marked-1, and2at every other vertex, so2is the side that holdsIndxNot[1]. - Write
0intoTat the three vertices ofCliq.
Arguments
Adj:N × Nadjacency matrix of the MPG. A non-zero entry is an edge.Cliq:3×1vector of node indices forming a 3-clique.
Returns
T::Vector{Int}:N × 1vector containing the adjacency number of each node:0for nodes in the clique,1for nodes in a disjoint component,2for nodes in the same component as the clique.
IndxNot::Vector{Int}:N × 1vector of nodes with no adjacencies to the clique.
Related
PortfolioOptimisers.BuildHierarchy — Function
BuildHierarchy(M::MatNum)Builds the predicted parent hierarchy for 3-cliques in a Maximal Planar Graph (MPG).
This function constructs the parent index vector (Pred) for each 3-clique, given the node-to-clique membership matrix M. It is a core step in the DBHT (Direct Bubble Hierarchical Tree) clustering pipeline, enabling the construction of the clique hierarchy tree.
Algorithm
- For each 3-clique
n, readChildren, the vertices that columnnofMmarks. - Sum the rows of
MoverChildren, and take asParentsevery clique whose sum equalslength(Children). Such a clique holds every vertex of cliquen, so it is a superset of it. Dropnitself from that list. - Set
Pred[n] = 0whenParentsis empty, which makes cliquena root. - Otherwise take the parent of the smallest vertex count, which is the smallest superset.
- Return an empty vector when two parents tie on the smallest count, which reports that no hierarchy was built. The loop stops at that point, so no later clique writes to the empty vector.
Arguments
M:N × Ncbinary matrix of node-to-3-clique memberships, whereM[i, n] = 1if nodeibelongs to 3-cliquen.
Returns
Pred::Vector{Int}:Nc×1vector of predicted parent indices for each 3-clique.Pred[n] = 0indicates a root clique. It is empty when step 5 of the algorithm fired.
Related
PortfolioOptimisers.AdjCliq — Function
AdjCliq(A::MatNum, CliqList::MatNum,
CliqRoot::VecNum)Find adjacent cliques to the root candidates in a Maximal Planar Graph (MPG).
This function computes the adjacency matrix among root candidate 3-cliques. Two root candidates are adjacent when they share exactly two vertices. Used internally by CliqueRoot with EqualRoot to construct a root from the adjacency tree of all root candidates.
A is read for its size and for nothing else, and no edge of the MPG reaches the answer. Nothing is lost by that. Every row of CliqList is a 3-clique of the MPG, so two rows that share two vertices both hold the edge between those two vertices. The count of the shared vertices is therefore the test for adjacency in the graph.
Algorithm
- Clear
Indicator, then mark in it the three vertices of root candidaten. - Read
Indicatorback at the three vertex columns of every root candidate, givingIndi. - Take the root candidates whose row of
Indisums to2, and set their entries of columnCliqRoot[n]ofAdjto one. - Repeat from step 1 for the next candidate. The test of step 3 is symmetric, so
Adjneeds no symmetrisation.
Arguments
A:N × Nadjacency matrix of the MPG. Onlysize(A, 1)is read, which sets the length ofIndicator.CliqList:Nc×3matrix. Each row lists the three vertices of a 3-clique in the MPG.CliqRoot: Vector of indices of root candidate cliques, indexing the rows ofCliqList.
Returns
Adj::SparseMatrixCSC{Int, Int}:Nc×Ncsymmetric adjacency matrix of the cliques.Adj[i, j]is one when cliquesiandjare both root candidates and share exactly two vertices. Every other entry is zero, so a clique that is not a root candidate carries an empty row and an empty column.
Related
PortfolioOptimisers.CliqueRoot — Function
CliqueRoot(::UniqueRoot, Root::VecNum, Pred::VecNum, Nc::Integer, args...)Construct the hierarchical adjacency matrix for 3-cliques in a Maximal Planar Graph (MPG) using the unique root selection method.
This method enforces a unique root in the clique hierarchy. If multiple root candidates are present, a synthetic root is created and all root candidates are attached to it. Used internally by CliqHierarchyTree2s when the root selection method is UniqueRoot.
Algorithm
- When more than one root candidate exists, append a synthetic clique to
Predand set the parent of every root candidate to it.Predis mutated in place, so the caller's vector gains that entry. - Allocate
HoverNc + 1rows and columns, which is the room the synthetic clique of step 1 needs. - Write
H[n, Pred[n]] = 1for every clique that has a parent. - Symmetrise
H.
Arguments
::UniqueRoot: Root selection method enforcing a unique root.Root: Vector of indices of root candidate cliques, indexing the entries ofPred.Pred:Nc×1vector of predicted parent indices for each clique.Nc: Number of 3-cliques.args...: Additional arguments (ignored for this method).CliqHierarchyTree2spasses the adjacency matrix and the clique list here, which theEqualRootmethod reads and this one does not.
Returns
H::SparseMatrixCSC{Int, Int}:(Nc + 1)×(Nc + 1)symmetric adjacency matrix representing the hierarchical tree of 3-cliques. Row and columnNc + 1hold the synthetic root, and they are empty when step 1 of the algorithm did not fire.
Related
CliqueRoot(::EqualRoot, Root::VecNum, Pred::VecNum, Nc::Integer,
A::MatNum, CliqList::MatNum)Construct the hierarchical adjacency matrix for 3-cliques in a Maximal Planar Graph (MPG) using the equal root selection method.
This method creates a root from the adjacency tree of all root candidate cliques, allowing for multiple equally plausible roots in the DBHT hierarchy. It is used internally by CliqHierarchyTree2s when the root selection method is EqualRoot.
Algorithm
- When more than one root candidate exists, build the adjacency
Adjamong the candidates withAdjCliq. One candidate alone has nothing to be joined to, soAdjis a zero matrix in that case, which makes step 4 add nothing. - Allocate
HoverNcrows and columns. This method adds no synthetic clique, so it needs no extra row. - Write
H[n, Pred[n]] = 1for every clique that has a parent. - Return a
0 × 0matrix whenPredis empty. Otherwise symmetriseHand addAdjto it, which joins the root candidates to each other.
Arguments
::EqualRoot: Root selection method that creates a root from the adjacency tree of all root candidates.Root: Vector of indices of root candidate cliques, indexing the entries ofPred.Pred:Nc×1vector of predicted parent indices for each clique.Nc: Number of 3-cliques.A:N × Nadjacency matrix of the MPG. It is forwarded toAdjCliq, which reads its size alone.CliqList:Nc×3matrix. Each row vector lists the three vertices consisting of a 3-clique in the MPG.
Returns
H::SparseMatrixCSC{Int, Int}:Nc×Ncsymmetric adjacency matrix representing the hierarchical tree of 3-cliques, or a0 × 0matrix whenPredis empty.
Related
PortfolioOptimisers.CliqHierarchyTree2s — Function
CliqHierarchyTree2s(Apm::MatNum, root::DBHTRootMethod = UniqueRoot())Construct the clique and bubble hierarchy trees for a Maximal Planar Graph (MPG) using the DBHT (Direct Bubble Hierarchical Tree) approach.
This function builds the hierarchical structure of 3-cliques (triangles) and bubbles from the adjacency matrix of a planar graph, supporting different root selection strategies via the root argument. It is a core routine for DBHT clustering and related phylogenetic analyses.
root is a positional argument, and every caller passes it positionally.
Algorithm
- Reduce
Apmto the binary adjacencyA, and list every 3-clique of it withclique3, givingCliqList. - For each 3-clique, cut it out of the graph with
FindDisjoint, which splits the vertices into the cliqueindx0, the sideindx1that the cut separated, and the sideindx2that stayed connected. - Take the smaller of the two sides, together with the clique, as the separated set
indx_s. A tie takesindx1, the side the cut separated. Recordlength(indx_s) - 3inSb[n], and mark the vertices ofindx_sin columnnofM. - Build the parent vector
PredfromMwithBuildHierarchy, and read the root candidatesRootoff it. - Build the clique hierarchy
HwithCliqueRoot, through the branchrootselects. - When
His non-empty, build the bubble hierarchyH2and the bubble membershipMbwithBubbleHierarchy, reduceH2to binary, and trimMbto the rows ofCliqList. - Return
0 × 0matrices forH2andMbwhenHis empty.
Arguments
Apm:N × Nadjacency matrix of the MPG, where nonzero entries indicate edges. Only the sparsity pattern is read, so a weighted matrix and its binary form give the same answer.root: Root selection method for the clique hierarchy.
Returns
H::SparseMatrixCSC{Int, Int}: Symmetric adjacency matrix representing the hierarchical tree of 3-cliques. Its size is set by theCliqueRootmethod thatrootselects.H2::SparseMatrixCSC{Int, Int}:Nb×Nbsymmetric adjacency matrix representing the bubble hierarchy tree, whereNbis the number of bubbles.Mb::Matrix{Int}: Bubble membership matrix for 3-cliques (Nc×Nb), whereMb[n, bi] = 1indicates 3-cliquenbelongs to bubblebi.CliqList::Matrix{Int}: List of 3-cliques (Nc×3), each row contains the vertex indices of a 3-clique.Sb::Vector{Int}:Nc×1vector indicating the size of the separating set for each 3-clique.
Related
References
- [52] W.-M. Song, T. Di Matteo and T. Aste. Nested hierarchies in planar graphs. Discrete Applied Mathematics 159, 2135–2146 (2011).
References
- [52]
- W.-M. Song, T. D. Matteo and T. Aste. Nested hierarchies in planar graphs. Discrete Applied Mathematics 159, 2135–2146 (2011).