2
$\begingroup$

I am looking for a simple boolean-like function which could check if a graph contains an even cycle. I'm aware there is ExtractCycles but I'm looking for a true/false output than a list of cycles.

$\endgroup$
2
$\begingroup$
evenCycleTest[g_: Graph] := AnyTrue[Flatten@FindCycle[g, #] & /@ Range[2, VertexCount[g], 2], # != {} &]

Examples:

g = Graph[RandomGraph[{20, 49}]];

evenCycleTest[g]

True

h = StarGraph[20];

evenCycleTest[h]

False

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.