Finding the dangling free part of a cluster connecting two nodes
Maybe this is what you are looking for?
SeedRandom[123]
n = 15;
m = 20;
(*conductances=1/RandomReal[{0,1},m];*)
conductances = ConstantArray[1., m];
G = RandomGraph[{n, m}, VertexLabels -> "Name"];
grad = With[{edges = UpperTriangularize[AdjacencyMatrix[G]]["NonzeroPositions"]},
With[{m = Length[edges]},
SparseArray @@ {Automatic, {m, n}, 0, {1, {
Range[0, 2 m, 2],
Partition[Flatten[edges], 1]
},
Flatten[Transpose[{ConstantArray[1., m], ConstantArray[-1., m]}]]}}
]
];
L = grad\[Transpose].DiagonalMatrix[SparseArray[conductances]].grad;
Now with source s
and target t
:
s = 1;
t = 2;
(* currents inserted at the nodes *)
Inodes = SparseArray[{{s}, {t}} -> {1., -1.}, {VertexCount[G]}, 0.];
a = SparseArray[ConstantArray[1., {1, n}]];
A = ArrayFlatten[{{L, a\[Transpose]}, {a, 0.}}];
S = LinearSolve[A];
(* potentials at the nodes *)
Unodes = S[Join[Inodes, {0.}]][[;; -2]];
(* currents through edges *)
Iedges = conductances grad.Unodes;
ϵ = 1. 10^-8;
stylefun = x \[Function] Directive[Thickness[0.0001 + x 0.02], Opacity[1.], ColorData["DarkRainbow"][x]];
Graph[G, EdgeStyle -> (
Thread[EdgeList[G] ->stylefun /@ Normalize[Threshold[Abs[Iedges], ϵ], Max]]
)
]
This paper was extremely helpful for me in order to set this up:
https://arxiv.org/pdf/1712.10263.pdf