classic DM algorithm implementation