3
$\begingroup$

How can I remove vertices from a graph that do not form a loop?
For example,

graph = Graph[{1 <-> 2, 2 <-> 3, 3 <-> 1, 4 <-> 5, 1 <-> 6, 5 <-> 7}, 
  VertexLabels -> "Name"]

enter image description here

The expected result would be something like this:

enter image description here

$\endgroup$

2 Answers 2

5
$\begingroup$

KCoreComponents

Subgraph[graph, KCoreComponents[graph, 2]]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ Nice, probably couldn't be simpler. $\endgroup$
    – internet
    Feb 16 at 7:29
3
$\begingroup$

I just found a way to do it. I'm still curious if there are other approaches or better improvements for my code, though.

graph = Graph[{1 <-> 2, 2 <-> 3, 3 <-> 1, 4 <-> 5, 1 <-> 6, 5 <-> 7}, 
   VertexLabels -> "Name"];
allVertex = VertexList[graph];
cycleVertex = 
  FindCycle[graph,Infinity, All] /. a_ \[UndirectedEdge] b_ -> Sequence[a, b] // 
    Flatten // DeleteDuplicates;
deleteVertex = Complement[allVertex, cycleVertex];
VertexDelete[graph, deleteVertex ]

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.