Optimal algorithms for local vertex quartet cleaning