5
$\begingroup$

I've written code to build an EdgeList where each element in the list is a node pair set. For example, given the list with the set of 5 node pairs below, the code correctly produces the undirected edge list {a<->b,c<->d,e<->f,g<->h} and the directed edge list {a->b,c->d,e->f,g->h}.

list = {{a, b}, {c, d}, {e, f}, {g, h}};
UndirectedEdge @@@ list
DirectedEdge @@@ list

Instead of sets of node pairs, suppose list = {{N1,N2,N4},{N1,N2,N3,N4}}. I would like the code I wrote to now produce the undirected edge list {N1<->N2,N2<->N4,N1<->N2,N2<->N3,N3<->N4} and the directed edge list {N1->N2,N2->N4,N1->N2,N2->N3,N3->N4}. Notice N1<->N2 (and N1->N2) appears twice. I can then run DeleteDuplicates to count repeated edges only once.

Any ideas would be most appreciated!!!

$\endgroup$

5 Answers 5

4
$\begingroup$

You can use Partition.

With

n = {{N1, N2, N4}, {N1, N2, N3, N4}};

Then

DeleteDuplicates@*UndirectedEdge @@@ Flatten[Partition[#, 2, 1] & /@ n, 1]
{N1<->N2,N2<->N4,N1<->N2,N2<->N3,N3<->N4}

Hope this helps.

$\endgroup$
1
  • 1
    $\begingroup$ Edmund: Very clever and thank u for sharing your insights!! ... 42,700 $\endgroup$
    – user42700
    Commented Jun 17, 2023 at 20:08
6
$\begingroup$
 (BlockMap[UndirectedEdge@@#&,#,2,1]&/@list)//Flatten//DeleteDuplicates

 (* {N1<->N2,N2<->N4,N2<->N3,N3<->N4} *)
$\endgroup$
1
  • 1
    $\begingroup$ Thank u, user1066. I was unaware of BlockMap ... thank u for sharing your code it really advances learning ... regards ... 42700 $\endgroup$
    – user42700
    Commented Jun 17, 2023 at 22:44
5
$\begingroup$
f0 = Apply[Union] @* Map[Partition[#, 2, 1, {1, -1}, {}, UndirectedEdge] &];

f0 @ list

enter image description here

f1 = Union @* Map[Splice @ Partition[#, 2, 1, {1, -1}, {}, UndirectedEdge] &];

f1 @ list

same result

$\endgroup$
4
  • 1
    $\begingroup$ kglr ... u r so gifted ... awesome solns!! $\endgroup$
    – user42700
    Commented Jun 17, 2023 at 23:23
  • $\begingroup$ f1 works in Vers. 13.2, but complains that there are too many arguments to Partition. Hard to understand why/how it works nonetheless. Seems useful to understand this since method f1 is fastest. $\endgroup$ Commented Jun 18, 2023 at 15:51
  • $\begingroup$ @StuartPoss, in version 13.1.0 (and earlier versions) it works without triggering an error message (although the sixth argument is highlighted red by syntax highlighter). If you call Partition with 7 or more arguments, (say, Partition[Range[10], 2, 1, {1, -1}, {}, Foo, Bar]) the error message says "Partition called with 7 arguments; between 2 and 6 arguments are expected". The sixth argument simply replaces List as the Head of partition elements; e.g., Partition[Range[10], 2, 1, {1, -1}, {}, Foo] $\endgroup$
    – kglr
    Commented Jun 18, 2023 at 16:14
  • $\begingroup$ It pays to go through ALL of the various signatures of Partition to take advantage of this advanced functionality. Negative values can produce complicated effects. However, if I use Partition[Range[10], 2, 1, {1, -1}, {}, Plus] it works as you indicate. If an additional function is substituted for Bar above, say Times, it doesn't. Sometimes, one needs to take more than baby steps to follow in the footsteps of giants. Thanks for the response. $\endgroup$ Commented Jun 19, 2023 at 3:15
2
$\begingroup$
f2 = Apply[Union] @* Map[EdgeList @* PathGraph];

f2 @ list

enter image description here

f3 = Apply[EdgeList @* GraphUnion] @* Map[PathGraph];

f3 @ list

same result

$\endgroup$
2
$\begingroup$
n = {{N1, N2, N4}, {N1, N2, N3, N4}};

Using SequenceCases

Union @ Flatten @
  Map[SequenceCases[#, x : {_, _} :> UndirectedEdge @@ x, Overlaps -> True] &, n]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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