Depth first search for reordering spreadsheet