Alternating Optimization for the Dense Subgraph and Submatrix Problems