There are several questions that seem close to this, but I haven't found any that are precisely what I need, which is called "path contraction."
Consider this graph:
mygraph =
Graph[{1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 3,
3 \[UndirectedEdge] 4, 4 \[UndirectedEdge] 5,
2 \[UndirectedEdge] 6},
VertexLabels -> "Name"]
I would like to eliminate all vertices that have a degree $2$... that is, vertices that are merely part of a linear path (in this case, vertices 3 and 4). My goal is to get the following graph:
I can find the vertices that have degree $2$ that should be deleted:
Select[VertexList[mygraph], VertexDegree[mygraph, #] == 2 &]
(* {3,4} *)
But when I try to delete these two (and preserve connectivity), I get this:
VertexContract[mygraph, {3, 4}]
which has the undesired remaining vertex between $2$ and $5$. I really want to contract vertices 3 and 4 and 5, but keep 5 labeled (and in its location).
Is there a single function that computes the graph I seek? Or is there an elegant way to compute it?
I'd also like to preserve the vertex coordinates of the remaining original vertices (e.g., 1,2,5,6). In short, I want to replace chains of edges by a single edge.
Note that for a general graph, the result is not a spanning tree. After all, I could have two densely connected subgraphs connected only by a chain of three edges. I'd like to replace that chain by a single edge connecting the two subgraphs.
IGSmoothen
to you when you were asking for visualizing some huge Collatz graph. $\endgroup$