3
$\begingroup$

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[02];

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]];
$\endgroup$

closed as off-topic by Szabolcs, m_goldberg, corey979, José Antonio Díaz Navas, J. M. will be back soon Oct 16 '18 at 5:28

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Szabolcs, corey979, José Antonio Díaz Navas, J. M. will be back soon
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ How do you call the function you have defined? $\endgroup$ – mikado Oct 13 '18 at 12:23
  • $\begingroup$ did you call the function? sourceANDsinkF[Gr,1,2] ?? $\endgroup$ – Ali Hashmi Oct 13 '18 at 12:23
  • $\begingroup$ @Mikado and @AliH: I call the function as sourceANDsinkF[Gr, 1, 2] for example. $\endgroup$ – Tugrul Temel Oct 13 '18 at 12:27
  • $\begingroup$ Voting to close as unreproducible per OP's answer. $\endgroup$ – Szabolcs Oct 13 '18 at 18:12
  • $\begingroup$ 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. $\endgroup$ – m_goldberg Oct 13 '18 at 20:57
2
$\begingroup$

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[2];

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]

enter image description here

{9, 11, 5, 14, 2, 6}

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

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – Tugrul Temel Oct 13 '18 at 14:42
  • 2
    $\begingroup$ "Does it mean that Mathematica 10 has a bug?" Probably yes. $\endgroup$ – Henrik Schumacher Oct 13 '18 at 14:43
  • $\begingroup$ 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'? $\endgroup$ – Tugrul Temel Oct 13 '18 at 14:56
  • 1
    $\begingroup$ 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.) $\endgroup$ – Henrik Schumacher Oct 13 '18 at 15:02
3
$\begingroup$

Remove the ";" at the end of Module[...], and it should work.enter image description here

$\endgroup$
1
$\begingroup$

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.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.