D-MG tradeoff and optimal codes for a class of AF and DF cooperative communication protocols