An algorithm is developed which allows calculating all ground states o
f ferromagnetic and unfrustrated antiferromagnetic Ising systems with
arbitrary site-dependent fields by transforming the system into an equ
ivalent network and calculating the maximal flow. By a trial and error
scheme a minimum cut is constructed which corresponds to a spin confi
guration. In this way each ground state is calculated with a finite pr
obability. The algorithm is applied to site-diluted antiferromagnets i
n external magnetic fields. It is found that in this case its time com
plexity is approximately quadratic in the lattice size. As an applicat
ion we calculate the distribution of overlaps between ground states of
the site-diluted antiferromagnet in a strong magnetic field and we an
alyse the fractal structure of these ground states.