2
$\begingroup$

Assuming I have a graph with a single cycle below, I want to list the vertices of this cycle in order. Whether it's clockwise (CW) or counterclockwise (CCW) is okay, as long as we are consistent. So the result could be one of something like this:
{9, 7, 8, 3, 1, 2, 4, 5, 6} (CW) or {9, 6, 5, 4, 2, 1, 3, 8, 7} (CCW), or any other sequence that starts from a different node.

cycle = {6 \[UndirectedEdge] 9, 9 \[UndirectedEdge] 7, 
   7 \[UndirectedEdge] 8, 8 \[UndirectedEdge] 3, 
   3 \[UndirectedEdge] 1, 1 \[UndirectedEdge] 2, 
   2 \[UndirectedEdge] 4, 4 \[UndirectedEdge] 5, 
   5 \[UndirectedEdge] 6};
Graph[cycle, VertexLabels -> "Name"]

enter image description here

$\endgroup$

2 Answers 2

3
$\begingroup$
FindHamiltonianPath[cycle]
{7, 9, 6, 5, 4, 2, 1, 3, 8}
$\endgroup$
3
$\begingroup$
cycle = UndirectedEdge @@@ 
   RandomSample[
    RandomSample /@ {{6, 9}, {9, 7}, {7, 8}, {8, 3}, {3, 1}, {1, 
       2}, {2, 4}, {4, 5}, {5, 6}}];
gr = Graph[cycle, VertexLabels -> "Name"]
FindCycle[gr][[1, All, 1]]

{5, 4, 2, 1, 3, 8, 7, 9, 6}

Talking about CW or CCW has no sense since it depends on graph layout.

$\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.