Introduction
Given an undirected graph G, we can construct a graph L(G) (called the line graph or conjugate graph) that represents the connections between edges in G. This is done by creating a new vertex in L(G) for every edge in G and connecting these vertices if the edges they represent have a vertex in common.
Here's an example from Wikipedia showing the construction of a line graph (in green).
As another example, take this graph G with vertices A, B, C, and D.
A
|
|
B---C---D---E
We create a new vertex for each edge in G. In this case, the edge between A and C is represented by a new vertex called AC.
AC
BC CD DE
And connect vertices when the edges they represent have a vertex in common. In this case, the edges from A to C and from B to C have vertex C in common, so vertices AC and BC are connected.
AC
/ \
BC--CD--DE
This new graph is the line graph of G!
See Wikipedia for more information.
Challenge
Given the adjacency list for a graph G, your program should print or return the adjacency list for the line graph L(G). This is code-golf, so the answer with the fewest bytes wins!
Input
A list of pairs of strings representing the the edges of G. Each pair describes the vertices that are connected by that edge.
- Each pair (X,Y) is guaranteed to be unique, meaning that that the list will not contain (Y,X) or a second (X,Y).
For example:
[("1","2"),("1","3"),("1","4"),("2","5"),("3","4"),("4","5")]
[("D","E"),("C","D"),("B","C"),("A","C")]
Output
A list of pairs of strings representing the the edges of L(G). Each pair describes the vertices that are connected by that edge.
Each pair (X,Y) must be unique, meaning that that the list will not contain (Y,X) or a second (X,Y).
For any edge (X,Y) in G, the vertex it creates in L(G) must be named XY (the names are concatenated together in same order that they're specified in the input).
For example:
[("12","13"),("12","14"),("12","25"),("13","14"),("13","34"),("14","34"),("14","45"),("25","45"),("34","45")]
[("DE","CD"),("CD","CB"),("CD","CA"),("BC","AB")]
Test Cases
[] -> []
[("0","1")] -> []
[("0","1"),("1","2")] -> [("01","12")]
[("a","b"),("b","c"),("c","a")] -> [("ab","bc"),("bc","ca"),("ca","ab")]
[("1","2"),("1","3"),("1","4"),("2","5"),("3","4"),("4","5")] -> [("12","13"),("12","14"),("12","25"),("13","14"),("13","34"),("14","34"),("14","45"),("25","45"),("34","45")]
[("1","23"),("23","4"),("12","3"),("3","4")]
, for which the output should presumably be[("123","234"),("123","34")]
, which cannot be correctly interpreted. I think the only way to fix this is to edit in a guarantee that the input will never contain such ambiguities, but if this question had been posted in the sandbox then I would have suggested being less prescriptive about the naming of vertices in the output. \$\endgroup\$