Use temporary table for node sorting to detect cycles