This work addresses the issue of imposing connectivity constraints in shape and topology optimization, with the goal of controlling the presence of void or solid islands in optimal designs. The approach relies on the spectrum of a two-phase differential operator with Dirichlet boundary conditions imposed on the external boundary. In contrast to previous works that adopt the same mathematical formulation for connectivity within the framework of density-based methods, we consider a level set representation of the domain combined with a topological-derivative-based optimization algorithm. Numerical experiments are presented to validate the strategy.