Yes
for i = 0; i N; i++
read in element x;element of vertexi[i] = x;position of vertex[i] = i;
Read in number of elements N
number of collisions reduce by 1;
Start
Error: exists a cycle in the graph
No
insert vertex[i] into min heap
number of collisions = -1
if element of vertex[i] 0
if number of collisions = 0
for i = 0; i number of vertices i++
ptemp = the closest collision element;ptemp = ptemp-next;(ptemp = last element of the current path)ptemp-next-element = element of current vertex[i];ptemp-next-position = i;(append the current vertex[i] to the end of the path)
yes
output = min of heap;delete min;
if output = -1
for every vertex that has vertex[i] as a collision
number of collisions of vertex[i] = (i - element of current vertex % N + N) % N
memory recycle
for j = 1; j number of collisions; j++
Halt