I want to delete all vertices of degree 2 from a graph and rewire it.
Given a graph like graphA, I want to obtain graphB.
edges1 = {1 <-> 2, 2 <-> 3, 3 <-> 4, 4 <-> 5, 5 <-> 6, 5 <-> 7, 1 <-> 9, 1 <-> 8}; graphA = Graph[edges1, VertexLabels -> "Name"] edges2 = {1 <-> 9, 1 <-> 8, 1 <-> 5, 5 <-> 6, 5 <-> 7}; graphB = Graph[edges2, VertexLabels -> "Name"] I have this simple algorithm, which I find easy to implement using a for loop in Python or Java.
- Get indices of all nodes with degree = 2.
- Pick a node of degree = 2 (call it N) and get its end points X and Y.
- Rewire X to Y, delete N
- Repeat 1 until there are no more nodes of degree 2
I know using a for loop would be painfully slow in big graphs. So what's the Mathematica-savvy way of doing this?





