2
$\begingroup$

I have a list of pairs in which every pair correspond to a edge in a network (or graph). This list in a .txt file (available here https://drive.google.com/file/d/1g-1l3__IxJ8qe_wtM4_0TYuklwoH_Rqa/view?usp=sharing). What I have to do is select a random number of nodes in the network and then extract from the list above the edges that leave and arrive at each node i randomly selected. I managed to do this with the following code:

links = Import["<https://drive.google.com/file/d/1g-1l3__IxJ8qe_wtM4_0TYuklwoH_Rqa/view?usp=sharing>", "Data"];
totalNodes = Max[First /@ links];
firstNode = Min[First /@ links];
randomNodes = RandomInteger[{firstNode, totalNodes}, 50];

Where I use the Max and Min functions to find the highest and lowest value attributed to a node in order to generate the list of randomly selected nodes using RandomInteger. Using the randomNodes list I have to find in the links list all the edges that go out from the node and also arrive at each of the nodes in randomNodes. The code below works fine for me:

selectedLinksOut = 
  Flatten[Table[
    Select[links, #[[1]] == Sort[randomNodes][[i]] &], {i, 
     Length[randomNodes]}], 1];

selectedLinksIn = 
  Flatten[Table[
    Select[links, #[[2]] == Sort[randomNodes][[i]] &], {i, 
     Length[randomNodes]}], 1];

finalLinks = Join[selectedLinksIn, selectedLinksOut];

But even for this set of data, which has about 25000 edges it takes a little bit to do this task. Would there be a way to do this maybe avoiding the use of Table to do it faster?

$\endgroup$
5
  • $\begingroup$ can you make the file public? $\endgroup$
    – kglr
    Jun 4, 2020 at 17:40
  • $\begingroup$ My bad, I think it's public now $\endgroup$ Jun 4, 2020 at 17:42
  • $\begingroup$ does List @@@ IncidenceList[UndirectedEdge @@@ links, Alternatives @@ randomNodes] give what you need? $\endgroup$
    – kglr
    Jun 4, 2020 at 17:56
  • $\begingroup$ .. or List @@@ EdgeList[UndirectedEdge @@@ links, UndirectedEdge[Alternatives @@ randomNodes, _]]? $\endgroup$
    – kglr
    Jun 4, 2020 at 18:00
  • $\begingroup$ Not exactly, this gives me a different output from finalLinks $\endgroup$ Jun 4, 2020 at 18:08

1 Answer 1

2
$\begingroup$
SeedRandom[1]
randomNodes = RandomInteger[{firstNode, totalNodes}, 50]

You can use Cases as follows:

lIn = Cases[{_, Alternatives @@ randomNodes}]@links;

Sort[lIn] == DeleteDuplicates@Sort[selectedLinksIn]
True
lOut = Cases[{Alternatives @@ randomNodes, _}]@links;

Sort[lOut] == DeleteDuplicates@Sort[selectedLinksOut]
True
lInOut = Union[lIn, lOut];

lInOut == DeleteDuplicates@Sort[finalLinks]
True

Alternatively,

lInOut2 = Cases[{Alternatives @@ randomNodes, _} | {_, Alternatives @@ randomNodes}]@
  links;

lInOut == Sort @ lInOut2
True
$\endgroup$
1
  • 1
    $\begingroup$ Oh, this one works a lot faster than the one I was using. Thanks ;) $\endgroup$ Jun 4, 2020 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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