An entanglement witness (EW) is an operator that allows the detection of en
tangled states. We give necessary and sufficient conditions for such operat
ors to be optimal, i.e., to detect entangled states in an optimal way. We s
how how to optimize general EW, and then we particularize our results to th
e nondecomposable ones; the latter are those that can detect positive parti
al transpose entangled states (PPTES's). We also present a method to system
atically construct and optimize this last class of operators based on the e
xistence of "edge" PPTES's, i.e., states that violate the range separabilit
y criterion [Phys. Lett. A 232, 333 (1997)] in an extreme manner. This meth
od also permits a systematic construction of nondecomposable positive maps
(PM's). Our results lead to a sufficient condition for entanglement in term
s of nondecomposable EW's and PM's. Finally, we illustrate our results by c
onstructing optimal EW acting on H = C-2 x C-4. The corresponding PM's cons
titute examples of PM's with minimal "qubit" domains, or-equivalently-minim
al;Hermitian conjugate codomains.