# How to Build EdgeList from a List

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!!!

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.

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

(* {N1<->N2,N2<->N4,N2<->N3,N3<->N4} *)

• Thank u, user1066. I was unaware of BlockMap ... thank u for sharing your code it really advances learning ... regards ... 42700 Commented Jun 17, 2023 at 22:44
f0 = Apply[Union] @* Map[Partition[#, 2, 1, {1, -1}, {}, UndirectedEdge] &];

f0 @ list


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

f1 @ list


same result

• kglr ... u r so gifted ... awesome solns!! Commented Jun 17, 2023 at 23:23
• 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. Commented Jun 18, 2023 at 15:51
• @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]
– kglr
Commented Jun 18, 2023 at 16:14
• 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. Commented Jun 19, 2023 at 3:15
f2 = Apply[Union] @* Map[EdgeList @* PathGraph];

f2 @ list


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

f3 @ list


same result

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


Using SequenceCases

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