building algorithm