Graph Traversal: private API
PortfolioOptimisers.distance_wei — Function
distance_wei(L::MatNum)Compute the shortest weighted path lengths between all node pairs in a network.
This function computes the distance matrix containing the lengths of the shortest paths between all node pairs in a (possibly weighted) network, using Dijkstra's algorithm. An entry [u, v] represents the length of the shortest path from node u to node v. The average shortest path length is the characteristic path length of the network.
Mathematical definition
\[\begin{align} D_{u,\,v} &= \underset{p \in P(u,\, v)}{\min} \sum_{(i,\,j) \in p} L_{i,\,j}\,, \\ B_{u,\,v} &= \left| p^{\star}(u,\, v) \right|\,. \end{align}\]
Where:
- $L_{i,\,j}$: Connection length of the pair $(i,\, j)$, the entry of the input matrix.
- $P(u,\, v)$: Set of the paths from vertex $u$ to vertex $v$.
- $p^{\star}(u,\, v)$: Shortest of those paths, and $\left| p^{\star}(u,\, v) \right|$ its edge count.
- $D_{u,\,v}$: Shortest path length from vertex $u$ to vertex $v$.
- $B_{u,\,v}$: Edge count of that shortest path.
A pair that no path joins keeps $D_{u,\,v} = \infty$, because the minimum over an empty set is an infinity.
Algorithm
- Set every entry of
Dtotypemax, its diagonal to zero, and every entry ofBto zero. - For each source
u, mark every vertex temporary inS, copyLintoL1, and set the frontierVto[u]. - Make the frontier permanent in
S, and zero the columns ofL1atV, so no edge re-enters a settled vertex. - For each vertex
vof the frontier, read its remaining neighboursTand take, entry by entry, the smaller ofD[u, T]andD[u, v] + L1[v, T]. - Where the second of the two won, set
B[u, T]toB[u, v] + 1, so the edge count follows the path that won. - Take
minD, the smallest entry ofD[u, :]over the temporary vertices. Stop when no temporary vertex remains, and stop whenminDis infinite, which leaves every unreachable vertex attypemax. - Set the new frontier
Vto every vertex at distanceminD, and repeat from step 3.
Arguments
L:N × Ndirected or undirected connection-length matrix.- Lengths between disconnected nodes should be set to
Inf. - Lengths on the main diagonal should be set to
0.
- Lengths between disconnected nodes should be set to
The input matrix must be a connection-length matrix, typically obtained by mapping weights to lengths (e.g., inverse of a similarity or correlation matrix). In weighted networks, shortest weighted paths may traverse more edges than shortest binary paths.
Returns
D::Matrix{<:Number}:N × Nshortest weighted path length matrix.D[u, v]is the length of the shortest path from vertexuto vertexv.B::Matrix{Int}:N × Nmatrix of the edge count of each shortest weighted path.
Related
PortfolioOptimisers.breadth — Function
breadth(CIJ::MatNum, source::Integer)Breadth-first search.
This function performs a breadth-first search (BFS) on a binary (directed or undirected) connection matrix, starting from a specified source vertex. It computes the shortest path distances from the source to all other vertices and records the predecessor (branch) for each node in the BFS tree. The tree holds one shortest path per reachable vertex, and not every shortest path, so branch reconstructs one route of minimum length rather than all of them.
Algorithm
- Colour every vertex white, set every entry of
distancetoInf, and set every entry ofbranchto zero. - Colour
sourcegrey, set its distance to zero and its branch to-1, and put it in the queueQ. - Take the head
uofQ, and read its out-neighboursnsfrom the stored entries of rowu. - For each neighbour
vwhose distance is still zero, set it todistance[u] + 1. The source starts at zero, so this arm also fires on the source itself as soon as one of its own neighbours is expanded. - For each white neighbour
v, colour it grey, setdistance[v]todistance[u] + 1, setbranch[v]tou, and appendvtoQ. - Drop
ufromQand colour it black. Repeat from step 3 untilQis empty.
distance[source] does not stay 0. Step 4 fires on the source itself the first time one of the source's own neighbours is expanded, so on an undirected graph without a self-loop the entry ends at 2, and at 1 where the graph carries a self-loop on the source. This is the behaviour of the MATLAB original. All three callers reset the entry after the call: FindDisjoint, DirectHb and BubbleCluster8s each write a 0 into it. Measured in issue #470 on a five-vertex path with a leaf, where the source reads 2.0.
Arguments
CIJ:N × Nbinary (0/1) connection matrix representing the graph. Rowuholds the out-neighbours of vertexu.source: Index of the source vertex from which to start the search.
Returns
distance::VecNum:N × 1vector of shortest path distances from the source to each vertex, in the type a division ofCIJ's entries lands in.Infmarks a vertex no path reaches. The source's own entry is not0; see the warning above.branch::Vector{Int}:N × 1vector of predecessor indices for each vertex in the BFS tree (-1for the source,0for an unreachable vertex).
Related