Can't this be optimized to only examine the incoming edges?