I have a digraph called weightedG
and like to derive some relations between flows.
SeedRandom[9];
n = 5;
d = 0.3;
G = RandomGraph[{Round[n], Round[n*(n - 1)*d]}, DirectedEdges -> True];
A = {{a1, a2, a3, a4, a5}, {b1, b2, b3, b4, b5}, {c1, c2, c3, c4,
c5}, {d1, d2, d3, d4, d5}, {e1, e2, e3, e4, e5}};
Ga = AdjacencyMatrix[G]*A;
sa = SparseArray[Ga];
weightedG =
Graph[sa["NonzeroPositions"], EdgeWeight -> sa["NonzeroValues"],
DirectedEdges -> True, VertexCapacity -> {i_ :> i},
VertexSize -> .12, EdgeLabels -> "EdgeWeight"];
SetProperty[weightedG,
VertexLabels -> {i_ :>
Placed[PropertyValue[{weightedG, i}, VertexCapacity], Center]}]
deneme[g_, s_, t_] := Module[{eList, source, sink},
eList = DeleteDuplicates[EdgeList@g];
source = Cases[eList, DirectedEdge[s, _]];
sink = Cases[eList, DirectedEdge[_, t]];
Equal @@ {Simplify@
Total[PropertyValue[{g, #}, EdgeWeight] & /@ source],
Simplify@Total[PropertyValue[{g, #}, EdgeWeight] & /@ sink]}];
deneme[weightedG, 3, 2]
yields:
Case 1: c5 == d2 + e2
Another case:
deneme[weightedG, 1, 5]
yields
Case 2: a4 == c5
and
Case 3: deneme[weightedG, 1, 6]
yields
a4 == 0
because the digraph has only 5 vertices
. In some case, such equations as in Case 3
may occur even when vertex 6
is a vertex in the digraph.
In this Module
I simply equalize the sum of the flow from source=3
to the sum of the flow into sink=2
. This is just an example, and one can play with alternative specifications.
My questions:
-- I like to formulate the Module
in the following format:
deneme=Module[(* the code *) ]&
and express deneme
as a function of three variables: deneme[weightedG, s, t]
as in the above examples.
-- For some vertices there may be no outgoing or incoming flows (as in Case 3
above), and these cases should be deleted from the flow calculations without interrupting the calculation.