0
$\begingroup$

This code here

Euleriano[g_] :=
 l = {FindEulerianCycle[g][[1]][[1]][[1]]} For[i = 1, 
    i < EdgeCount[g] + 1, i++, 
    AppendTo[l, 
     FindEulerianCycle[g][[1]][[i]][[2]]]] Block[{grafo = g, inizio, 
     cammino}, inizio = FindEulerianCycle[grafo][[1]][[1]][[1]];
    cammino = l;
    ListAnimate[
     Table[Graph[grafo, 
       VertexStyle -> 
        Append[Map[Rule[#, Pink] &, Union[cammino[[1 ;; v]]]], 
         cammino[[v]] -> Red], 
       EdgeStyle -> 
        Evaluate[(UndirectedEdge[#1, #2] -> Directive[Red, Thick]) & @@@
           Partition[cammino[[1 ;; v]], 2, 1]], 
       VertexSize -> Medium], {v, Length[cammino]}]]]

finds and animates an eulerian cycle on an input graph, provided it has one. The problem is this animation always misses the last edge of the cycle. How can I solve this? For clarification, cammino=path, inizio=start, grafo=graph.

$\endgroup$
0
$\begingroup$

After fixing some typo in your code. It seems to run fine to me:

This code here

Euleriano[g_] :=
 (l = {FindEulerianCycle[g][[1]][[1]][[1]]}; For[i = 1, 
    i < EdgeCount[g] + 1, i++, 
    AppendTo[l, 
     FindEulerianCycle[g][[1]][[i]][[2]]]] Block[{grafo = g, inizio, 
     cammino}, inizio = FindEulerianCycle[grafo][[1]][[1]][[1]];
    cammino = l;
    ListAnimate[
     Table[Graph[grafo, 
       VertexStyle -> 
        Append[Map[Rule[#, Pink] &, Union[cammino[[1 ;; v]]]], 
         cammino[[v]] -> Red], 
       EdgeStyle -> 
        Evaluate[(UndirectedEdge[#1, #2] -> Directive[Red, Thick]) & @@@
           Partition[cammino[[1 ;; v]], 2, 1]], 
       VertexSize -> Medium], {v, Length[cammino]}]]])

You could also use Animate and HighlightGraph for simpler code:

AnimateEulerican[g_] :=
 With[{l = First[FindEulerianCycle[g]]},    
  Animate[HighlightGraph[
    g, {Style[Graph[l[[1 ;; n]]], Directive[Pink, Thick]], 
     Style[l[[n, 2]], Red]}, VertexSize -> {_ -> Medium}], {n, 1, 
    Length[l], 1}]]
$\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.