# What is wrong with Module? or me? [closed]

The following code does not give me an answer. It looks all is working when each command inside Module is individually run. But as a Module, it does not.

Can you tell me what is wrong with my formulation?

SeedRandom;

Gr = RandomGraph[{6, 10}, DirectedEdges -> True, VertexLabels -> "Name"];

sourceANDsinkF[G_, s_, t_] := Module[{e, source, sink},
e      = DeleteDuplicates[EdgeList[G]];
source = Cases[e, s \[DirectedEdge] _];
sink   = Cases[e, _ \[DirectedEdge] t]];

• How do you call the function you have defined? – mikado Oct 13 '18 at 12:23
• did you call the function? sourceANDsinkF[Gr,1,2] ?? – Ali Hashmi Oct 13 '18 at 12:23
• @Mikado and @AliH: I call the function as sourceANDsinkF[Gr, 1, 2] for example. – Tugrul Temel Oct 13 '18 at 12:27
• Voting to close as unreproducible per OP's answer. – Szabolcs Oct 13 '18 at 18:12
• I'm voting to close this question as off-topic because the OP's problem can not be reproduced and the OP indicates he now believes it was a local system issue. – m_goldberg Oct 13 '18 at 20:57

## 3 Answers

You might be also be interested in using the IncidenceMatrix matrix A = IncidenceMatrix[Gr] of the graph. This should be much more efficient.

One has A[[i,j]] == 1 if edge j is ingoing into vertex i; A[[i,j]] == -1 if edge j is outgoing from vertex i; and A[[i,j]] == 0 if edge j otherwise.

So you get the sums of ingoing resp. outgoing edge weigths for all vertices at once as follows:

SeedRandom;

n = 6;
m = 10;
Gr = RandomGraph[{n, m},
DirectedEdges -> True,
VertexLabels -> "Name",
EdgeWeight -> RandomInteger[{1, 10}, m],
VertexLabels -> "Name",
EdgeLabels -> "EdgeWeight"
];

outgoingweights = UnitStep[-IncidenceMatrix[Gr] - 1].PropertyValue[Gr, EdgeWeight]
ingoingweights = UnitStep[IncidenceMatrix[Gr] - 1].PropertyValue[Gr, EdgeWeight] {9, 11, 5, 14, 2, 6}

{9, 10, 6, 8, 14, 0}

• Your answer works very well, but I still want to know why I cannot use Module posted in my question, while everybody else can use it. Does it mean that Mathematica 10 has a bug? – Tugrul Temel Oct 13 '18 at 14:42
• "Does it mean that Mathematica 10 has a bug?" Probably yes. – Henrik Schumacher Oct 13 '18 at 14:43
• In your code, you find the sum of edge weights for all vertices. Is there a way to limit the sum of edge weights to a single s' and a single t'? – Tugrul Temel Oct 13 '18 at 14:56
• Of course: To the same with IncidenceMatrix[Gr][[s]] and IncidenceMatrix[Gr][[t]], respectively. (This will work as long as you use integer values running from 1 to n as vertices. Otherwise, you have to compute the vertex indices first.) – Henrik Schumacher Oct 13 '18 at 15:02

Remove the ";" at the end of Module[...], and it should work. This Module works just fine, and nothing wrong with it. Apparently, there was internally something wrong with my computer. When I accidentally exited Mathematica and run it again, the above code started to work. So, as I suspected, there was something wrong on my side.