This work investigates the general problem of phase unwrapping for arbitrary N-dimensional phase maps. A cost function-based approach is outlined that leads to an integer programming problem. To solve this problem, a best-pair-first region merging approach is adopted as the optimization method. The algorithm was implemented and tested with 3D MRI medical data for venogram studies, as well as for fMRI applications in EPI unwarping and rapid, automated shimming. Magn Reson Med 49:193–197, 2003. © 2003 Wiley-Liss, Inc.