We develop an algorithm for the automatic generation of a switching automaton that stabilizes a given discrete-time switched linear system. The algorithm iteratively checks the stability of the system constrained by the current automaton and modifies the automaton's graph, until a stabilizing solution, or the empty graph, is reached. Stability is checked by means of a recently developed direct algorithm, that in case of instability provides a graph's cycle with unstable corresponding matrix product. Our heuristic to modify the switching graph limits the number of consecutive repetitions of the unstable periodic path. It is based on an ergodic result that tightly lower bounds the system's Constrained Joint Spectral Radius - the largest long-...